File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Real use of finally block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Real use of finally block" Watch "Real use of finally block" New topic
Author

Real use of finally block

Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

What is the real use of finally block in try-catch-finally?
(Anything inside finally executes anyway..right?!)
If an exception is caught in a catch block,anyway the contents/lines below the block is going
to execute..Then what is the specific need of finally block?


OCPJP 6
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Well what if you are rethrowing the exception that you have caught then i don't think any line below the catch block will run. But finally will always run even if you are rethrowing the exception that has been caught.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

There doesn't even have to be a catch block, you can just have try-finally:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Yeah try alone cannot exist. try should be paired either with catch or finally or both in the order try catch finally.
Jaydeep Mazumdar
Greenhorn

Joined: Aug 26, 2009
Posts: 23
Consider the case when your code throws some un-anticipated exceptions (e.g unchecked exceptions like NullPointerException) which are not explictly caught in the catch block.

In such a case, since the exception is not caught, the program will dump the trace and exit. However, if you have some cleanup to be done, you can do it in the finally block.

Infact, you can also have only try and finally blocks. e.g.

Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

Yeh the finally block would usually be used some cleanup code.

For example if you are reading from a database or from a file you would want to ensure that the connections to the file/database are closed.
You would want to do this if the application completes normally or if there was an exception thrown... so in this case you would use the finally block.

Sean


I love this place!
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Well yeah closing a particular application is a good clean up but we have to see the closing method does not throw a checked exception which will land another try catch in your finally block. This happens in the file operations when you try to put the close method in the finally block without handling or declaring the checked exception. So we need to keep special watch on the clean up codes that we put in the finally block.
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

thank you all...thanks for all your nice explanations..!
so is it ok if we put another try-catch-finally inside finally....because connection.close() - clean up requires exception to be caught or thrown..
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Why not try it out writing a program because you will remember that. I guess you will find the answer there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Real use of finally block
 
Similar Threads
Execute java statement after exception
On Exception Handling
Try, Catch, Finally
try without catch?
Can we use finaly block to catch exceptions without using catch block?