aspose file tools*
The moose likes Beginning Java and the fly likes What is the exact use of a finally block in exception handling? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the exact use of a finally block in exception handling?" Watch "What is the exact use of a finally block in exception handling?" New topic
Author

What is the exact use of a finally block in exception handling?

rakesh kadulkar
Ranch Hand

Joined: Jul 24, 2008
Posts: 115
Hi

I want to know the exact scenario where I can use a finally block in exception handling? Please explain with an example.


Rakesh Kadulkar
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38357
    
  23
Java™ Tutorials.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
The most popular example is a database connection. The finally block WILL run even if there are tons of exceptions happening in your try block. Since you want to close your database connection cleanly, this is to be done in the finally block, whether there have exceptions been raised or not.


JDBCSupport - An easy to use, light-weight JDBC framework -
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Key benefits of using finally have already listed.

Suppose you have a code

Now, many times the catch block re throw an exception and goes out of the current function. In that case the function "funcMustExecute" will not execute. In order to ensure that you function get execute you should write it in the finally block.
Hope it helps.


http://muhammadkhojaye.blogspot.com/
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: What is the exact use of a finally block in exception handling?