• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Hierarchy in Exceptions

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code which does not compile. Gives a compilation error: C:\JavaExamples\SL275Lab\mod01\examples\PassA.java:25: 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!
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.

Mathangi.
 
Nina Binde
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Thanks!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Nina Binde
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got you Barry! Thanks..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic