Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Hierarchy in Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Hierarchy in Exceptions" Watch "Hierarchy in Exceptions" New topic

Hierarchy in Exceptions

Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
I have the following code which does not compile. Gives a compilation error: C:\JavaExamples\SL275Lab\mod01\examples\ exception Level2Exception is never thrown in body of corresponding try statement
catch (Level2Exception e) {
1 error

When I interchange the catch statements, it compiles fine like below:

I fail to understand why in the first case was it complaining that Level2Exception is never thrown in the body of the corresponding try statement when I clearly have this in the try code:

Or is it throwing the compilation error because the exception heirarchy is violated since Level1Exception is caught before Level2Exception is caught? In that case, the error message should be different than what I get.

Any thoughts on this is appreciated. Thank you!
Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
In the first case even if a level2exception is thrown it will be caught by the catch clause. Since it is the superclass all exceptions will be caught. In a sense no exception will come downwards. When u interchange the code if a level1exception comes the first catch clause cant catch it and it will come downwards. That is why the compiler is not complaining. But i dont know y the " Level2Exception has already been caught" exception is not coming. or y there is no error if you give Level2Exception in both catch clauses
Mathangi Shankar
Ranch Hand

Joined: Nov 01, 2004
Posts: 56

Please use the following code in the main try block:

throw new Level2Exception();

Now the code will compile fine. The problem is not due to hierarchy of exceptions.

The Level2Exception was never thrown in the main try block.


--------------------------------<br />SCJP1.4
Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
hmmm..I do throw the Level2Exception in the inner try block which is part of the main(enclosing) try block, isn't it? It should still consider it as thrown and not give me compile error.
Why then would it work for Level1Exception as seen in case 2?

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I'm talking about the first posted code example.
In the innermost try you are catching Level1Exception. That means Level1Exceptions and its subclasses will be caught.
So a thrown Level2Exception will never be thrown up to the outer try's catch clauses, because it is already handled by the innermost try's catch clause.

If after the "b++;" you put "throw e;" you will get a successful compilation.
[ November 04, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
Got you Barry! Thanks..
I agree. Here's the link:
subject: Hierarchy in Exceptions
It's not a secret anymore!