• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Exception Handling Query

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,


I have the following program



when x= 1 the switch jumps tp case 1 and line (2) is executed and goes to
// line (6) and then d=1. My doubt is The nested catch clause will catch a Level1Exception or any subclass of it. So Level2Exception should also be caught and then b should be equal to 1 .But when I run the program b is not incremented ?. ofcourse all finally block runs so c=g=1.

I have one more question when x=3 it // line (7) should be executed but the result is same as when x=2. why is it so.


please expain me where am I going wrong.

Thanks in advance.

[EJFH: Added CODE tags ]
[ February 22, 2005: Message edited by: Ernest Friedman-Hill ]
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(I added CODE tags to your code -- see how nice it looks now? On the message-entry screen, look at the "Instant UBB Code" buttons.)

OK, now -- the nested catch clause won't catch Level1Exception and all subclasses, it will catch Level2Exception and all subclasses. But when x is 1, a Level1Exception is thrown, and the nested catch block is not used.

Now, when x is 2, that catch block is indeed used; but then the outer one isn't. If you want the outer one to catch an already-caught exception, then the inner-catch block has to re-throw the exception -- i.e.,

catch (Level2Exception e) {b++; throw e;}

Once you catch an exception, unless you rethrow it, it's "dead". It can't be caught again.
 
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic