• 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

Purpose of Finally Block

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the purpose of finally block (i know Mandatory statements are written in finally block)..


But outside of the catch block statements are also executed.i write mandatory statements out side the catch block.



So why do we need this finally block?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code outside the finally block is not guaranteed to execute. Let's say some sort of unchecked exception happened that wasn't caught, or somewhere in the try block you execute the return statement; then code below the finally block would not be executed. Code inside the finally block is *always* executed, even if an exception was raised and left uncaught, or if the method returned abruptly.
 
Raj Srimandal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if there was a exception in finally block itself?

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can nest try-catch-finally blocks in a finally block to handle any exceptions that occur on that level as well.
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also perform some operations like DB connection closing, flushing etc in finally block
like con.close();
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Srimandal wrote:What if there was a exception in finally block itself?


Hmm... that is your responsibility to code to not throw any exception from finally block.
for an example:

if an exeception is thrown before close(), then dbConnection close is failed. then dbConnection object can not be garbage collected, since it is a system resource. so probably you need to wrap try catch around the exception and log the error message like below

rule of thumb:do not throw an Exception and do not use return statement in finally block.finally block should be execute normally.
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Code inside the finally block is *always* executed


Unless System.exit() is executed in either the try or catch block.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Christophel wrote:

Stephan van Hulst wrote:Code inside the finally block is *always* executed


Unless System.exit() is executed in either the try or catch block.



... or System.exit() is called in another thread, or this try/finally is in a daemon thread and all the non-daemon threads complete, or the JVM dumps core, or someone pulls the plug, or a meteor destroys the earth, or...

There are an infinity of scenarios in which it won't run, so just enumerating one of them opens the door to a can of incompletion and confusion-shaped worms.

A more precise and cover-all-the-bases way to put it would be something like, "As long as the thread continues executing, finally is guaranteed to execute after the try and a catch block if one was invoked, before transferring control to the next statement or back up the stack to the calling method."
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you mention meteors, you must link back to the late Stan James’ post about them.
 
reply
    Bookmark Topic Watch Topic
  • New Topic