This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

finally block in Exceptions

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any body explain the exact usage of finally block.

Please explain me with an real time example.



Thanks.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!
 
Sheriff
Posts: 22506
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 74354
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22506
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course I was only referring to software issues
 
reply
    Bookmark Topic Watch Topic
  • New Topic