*
The moose likes Beginning Java and the fly likes Purpose of Finally Block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Purpose of Finally Block" Watch "Purpose of Finally Block" New topic
Author

Purpose of Finally Block

Raj Srimandal
Greenhorn

Joined: Mar 13, 2008
Posts: 10
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?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

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

Joined: Mar 13, 2008
Posts: 10
What if there was a exception in finally block itself?

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

You can nest try-catch-finally blocks in a finally block to handle any exceptions that occur on that level as well.
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
You can also perform some operations like DB connection closing, flushing etc in finally block
like con.close();


Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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.
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 237
    
    1

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

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
If you mention meteors, you must link back to the late Stan James’ post about them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Purpose of Finally Block