• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finally block in Exceptions

 
kiranb reddy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any body explain the exact usage of finally block.

Please explain me with an real time example.



Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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 ..
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course I was only referring to software issues
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic