File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception subclass" Watch "Exception subclass" New topic
Author

Exception subclass

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479


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

Joined: Aug 31, 2006
Posts: 226
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

Joined: Jan 05, 2006
Posts: 479
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

Joined: Aug 31, 2006
Posts: 226
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

Joined: Jan 05, 2006
Posts: 479
Thank you very much Tim.Now... i got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception subclass