• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Exception Ambiguity

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does this compile ?





And the code below does not.




The only change between the two code snipnets is that in the first one both catch block catch level2Exception and in the second one level1Exception.
[ April 24, 2006: Message edited by: Amit Goyal ]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
how the compiler thinks for this is:
in case of the first code , if case 1 is true , level1Exception is throwed , is tehre a handle to it or not.it cannot be hanled by first try so the exception propogates to 2nd try , where it is caught by Exception.
if ase 2 is true , then it is handles by inner try where it catches level2.

now in the 2nd code snippet , both case 1 and case 2 are handled by the catch of the inner try.
so the outer catch for level2 is never reached.so we are trying to handle something which is never occuring.so the compiler scolds us.
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Please see my comments in below code


Hope it helps
 
Amit Goyal
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understant why compiler gives error in the second code sniplet, but why does the first one compile.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to these exception question is that
if you have nested try which throws the exceptions
then their must be atleast one exception that can't be handled
by the inner catch and propagated to the outer catch.

For exampe if you have
Exception <-- E1 <--E2 then

The final rule is that of the thrown exceptions
from the inner try their should be atleast one exception that should
be able to propagate to outer catch.

Please correct if wrong
[ April 25, 2006: Message edited by: Shaliey Gowtham ]
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic