aspose file tools*
The moose likes Beginning Java and the fly likes finally block in Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "finally block in Exceptions" Watch "finally block in Exceptions" New topic
Author

finally block in Exceptions

kiranb reddy
Greenhorn

Joined: Sep 28, 2008
Posts: 20
Can any body explain the exact usage of finally block.

Please explain me with an real time example.



Thanks.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

finally blocks ensure that some piece of code will be executes in ANY situation. Even if an exception occurs, the code in the finally block wil be executed. For example, it is useful if you open a connection to a database in a method and you want to guarantee that the connection gets closed when the method end.

Here, the connection will always be closed, even if a RuntimeException is thrown in the try block.


[My Blog]
All roads lead to JavaRanch
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

"finally" block will get executed , whether or nor an exception is thrown !

I used to close an important resources which may left open , If not properly closed , like when you get JDBC connection , Its a best practice to close them in finally block ,



Hope this help ..


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by Christophe Verre:
finally blocks ensure that some piece of code will be executes in ANY situation. Even if an exception occurs, the code in the finally block wil be executed. For example, it is useful if you open a connection to a database in a method and you want to guarantee that the connection gets closed when the method end.

Here, the connection will always be closed, even if a RuntimeException is thrown in the try block.


A 4 minutes late .. A Data redundancy occurred !!!

This DB connection is replaced by any Data stream or network connection !!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Besides JDBC connections, this approach should also be used for Readers, Writers, Streams and Sockets.

The reason is that the finally block is guaranteed to run*, so the connections etc will be closed no matter what.


* There are two known exceptions which can cause a finally block to be skipped:
1) a call to System.exit
2) a JVM crash, for example because of a bug in a native library


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
You also use a finally for unlocking a Lock.

Rob: Ken Blair and the late Stan James showed a few other occasions where a finally may not be executed in this old thread.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Of course I was only referring to software issues
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finally block in Exceptions