• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception subclass

 
Shiva Mohan
Ranch Hand
Posts: 484
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Exception subclass is Level1Exception in the above program. Right? Then why, when I throw the Exception it is not catch in the Level1Exception catch clause. Instead it is showing the error �Exception must be caught or declared to be thrown�. I am asking when do we use the class and subclass exceptions rules and get substituted. Please Explain.
Shiva.
 
Tim LeMaster
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Level1Exception IS-A Exception
Level2Exception IS-A Level1Exception
Level2Exception IS-A Exception

Can you explain why you think catch Level1Exception will catch a java.lang.Exception?
 
Shiva Mohan
Ranch Hand
Posts: 484
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Level1Exception is a subclass of Exception.So i am thinking either the Exception or its subclasss(Level1Exception) of catch clause can handle the throwed Exception(Exception).

[ September 25, 2006: Message edited by: Shiva Mohan ]
[ September 25, 2006: Message edited by: Shiva Mohan ]
 
Tim LeMaster
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then your understanding needs to be reversed.



This will catch the Level1Exception because Level1Exception IS-A exception.
But the reverse is not true - catch (Level1Exception e) will not and should not catch java.lang.Exception.

A catch block will catch that Exception and all its subclasses. So catch (Level1Exception e) will catch Level2Exceptions not java.lang.Exceptions.
[ September 25, 2006: Message edited by: Tim LeMaster ]
 
Shiva Mohan
Ranch Hand
Posts: 484
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Tim.Now... i got it.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic