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.
Can you explain why you think catch Level1Exception will catch a java.lang.Exception?
Joined: Jan 05, 2006
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 ]
Joined: Aug 31, 2006
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 ]