This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
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: 39478
    
  28
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/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the exact use of a finally block in exception handling?