• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Exception in finally clause

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another question from the Sun Certified Java Essentials link. It states:


finally blocks execute no matter what, in other words, whether the code executes without an exception, or an exception is thrown and successfully caught, or an exception is thrown and not caught. This is useful because, except for code in a finally block, if an exception is thrown and not caught, the execution of the rest method is abandoned



I created the program below to test the error process in the finally clause as listed below:


When it runs, I get the Arithmetic error and the "After second error" line doesn't print. Does this mean that if an error is thrown in the finally block and not caught, it would also abandon executing the rest of the code as it does for other try blocks or is it just because I am throwing a deliberate Runtime exception instead of a checked exception? I guess what I am saying is that it appears as if the finally block is subject to the same code abandonment rules as the rest of the try blocks. Does this mean you should be careful with your code in a finally block to make sure you either don't throw any exceptions or catch them in try/catch/finally constructs?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right finally is a block of code which like any block of code can throw exception and rest of the statements in the block are not executed
 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regardless of where the exception occurs, the JVM will handle it in a consistent manner, so yes, the code in a finally block is subject to the same exception handling logic as any other program block. Naturally, it would not be good programming practice to deeply nest try/catch/finally blocks, so you'll want to limit the amount of logic you place in your finally block -- perform only the essential "clean-up" needed so your program may "gracefully" (hahah, don't we wish), recover.
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, for me, the most often encountered exception in the finally block is the infamous NullPointerException

Keep a look out for codes that could gives you that
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic