• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Crettaz's mock exam q46

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the code fragment:

the correct answer : prints nothing
i answered that it'll givecompile error since i was thought that the code in the try block will not throw any checked exception.
but after i saw the right answer ,i touht that since the second catch clause catches Exception in general (not specific checked exception) it'll not give error.
Am i right?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that code, an ArithmeticException is thrown and then caught by the matching catch clause. Within that block, another exception is thrown, which would be thrown to the invoking method. In most cases, this would cause the application to bail and you'll end up with a stack trace. However, in any try/catch/finally series, the finally block is always executed. In this case, the finally block tells the application to return normally, which causes the application to disregard the exception.
Here's a snippet from Val's own explanation:


...By executing a return, the finally block discards the exception that was to be thrown and the code returns normally without even printing a tiny bit of the stack trace. Remember that the finally block always has the last word. you can put logic in there that actually decides whether it is worth throwing the exception or simply discarding it.


Hope that helps,
Corey
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i answered that it'll givecompile error since i was thought that the code in the try block will not throw any checked exception.
but after i saw the right answer ,i touht that since the second catch clause catches Exception in general (not specific checked exception) it'll not give error.
Am i right?


a)The try{} block is not throwing any checked exception.
b) Had you catched a checked exception you would have got compiler error saying Exception xxx is never thrown from try{} clause.
c) Had the order of the two exceptions are reversed you would have got compiler error saying that RunTimeException is already causght as it is a sub class of Exception.
d) You can always catch Exception even when you are not throwing any thing from your try.
e) The return in finally block is making the compiler discard the new Exception thrown from first catch block and coming out without any output as if every thing went normally.
[ March 14, 2003: Message edited by: Sarma Lolla ]
reply
    Bookmark Topic Watch Topic
  • New Topic