File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Confusion on when to use Try-Finally combination instead of Try-catch or Try-catch-finally? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Confusion on when to use Try-Finally combination instead of Try-catch or Try-catch-finally?" Watch "Confusion on when to use Try-Finally combination instead of Try-catch or Try-catch-finally?" New topic
Author

Confusion on when to use Try-Finally combination instead of Try-catch or Try-catch-finally?

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 541
I know that try should be followed with atleast a Catch block or Finally Block or Catch-Finally. I understand Try-Catch scenario and have used it.I also understand Try-Catch-Finaly Scenario but I am confused about the third one and have never used it: Try-Finally.
thanks.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
You use a try/finally when the code in the try block might throw an exception, but you don't want to handle that exception (you're happy for it to propagate back up through the call stack), but you do want to make sure that some code is executed whether an exception is thrown or not (the finally block).
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 481
    
    1

Hello Monica!

Especially for you I opened Bruce Eckel "Thinking in Java" and found the following.

1. Block finally should be used to return program into some initial state after exception was arised.

2. In block finally you can lost information about exception arised in try block if code in block finally threw exception too.

3. In block finally you can lost information about exception arised in try block if you used keyword return in finally block.


True person is moral, false is right!
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1268

Try-finally can be used when you don't want to catch exception instead of which you want other methods to provide catch statement when they use this method.




Now it can bound to provide exception handling to the method from where it is being used. So here you can push Exception handling to other level


No Kaustubh No Fun, Know Kaustubh Know Fun..
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 481
    
    1

Also note that finally block is executed after keywords break, continue, return. Finally is executed regardless if exception was caught or not!
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 865
    
    5
I know that try should be followed with at least a Catch block or Finally Block or Catch-Finally.


The new try-with-resources (since version 1.7) can stand alone, that is, without both catch and finally.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 541
thanks all

Try-finally can be used when you don't want to catch exception instead of which you want other methods to provide catch statement when they use this method.
row

But this is what we achieve with THROWS clause? then what different do we actually achieve using try-finally?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1939
    
  28
Here are a couple of examples.

1. You have a method that's open a file for reading, reads from the file and then closes the file. At various stages in the process exceptions may be thrown and you probably wouldn't want that method to handle the exceptions and so you let them propagate up the call stack. This is all well and good but if an exception is thrown after the file has been opened you will never close the stream down and so you tie up some of your precious resources. To solve this (prior to Java 7) you wrap the code in a try block and put the call to close the file input stream in the finally clause. Now however the method exits be it normally, through a return statement or an exception the stream will be closed.

2. You have a GUI application that has a long running task and so you want the cursor to change to a timer symbol whilst the task is running. The task code may thrown an exception but you want to handle it in the calling method. The problem now is if an exception is thrown after changing the cursor to a timer but before changing it back to a pointer, the cursor will never get changed back to a pointer. The solution is to wrap the code in a try block and put the code to change the cursor back to a pointer in the finally clause.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7052
    
  16

Monica. Shiralkar wrote:but I am confused about the third one and have never used it: Try-Finally.

Just to add to all the good advice:

There is one specific case (although it's quite specialized and you probably haven't used it yet) that requires try...finally. ReentrantLock's.
The syntax is specifically:
Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 541
thanks all


You have a method that's open a file for reading, reads from the file and then closes the file. At various stages in the process exceptions may be thrown and you probably wouldn't want that method to handle the exceptions and so you let them propagate up the call stack. This is all well and good but if an exception is thrown after the file has been opened you will never close the stream down and so you tie up some of your precious resources. To solve this (prior to Java 7) you wrap the code in a try block and put the call to close the file input stream in the finally clause. Now however the method exits be it normally, through a return statement or an exception the stream will be closed.


It appears we are talking about letting the exception propogate which means in the method specify throws clause and let it propogate. It would be handled using try catch in some other layer.So if it would be handled in some other layer why is finally required in this current layer.

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1939
    
  28
So if it would be handled in some other layer why is finally required in this current layer.

Because if there is no catch or finally clause and an exception is thrown the method execution stops at that point so you wouldn't get to close the stream/set the cursor back to a pointer/release the lock or whatever else it is you have to do to before leaving the method. If there is a finally clause the code in the clause is executed even if an exception is thrown.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18126
    
    8

Just to put everything in this post a bit differently:

When you're using a try-block, there are two things which have to be taken care of:

1. Deal with exceptions if they are thrown

2. Clean up resources when you're finished

As you know by now, (1) is what the catch-block is for and (2) is what the finally-block is for. Of course it's possible there isn't any cleanup to be done, in which case there's no finally-block. And it's possible that there aren't any exceptions which this module is capable of dealing with, in which case there's no catch-block.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

Monica

Using try-finally is like saying: "No matter what happens in one piece of code (the try block), I always want another operation(s) to be executed after"

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 541
thanks all

"No matter what happens in one piece of code (the try block), I always want another operation(s) to be executed after"


So does that mean , try finally will be inside a method where throws clause is there and the layer from which this method will be called will have a try block again?

James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

So does that mean , try finally will be inside a method where throws clause is there and the layer from which this method will be called will have a try block again?


If the try finally method throws a checked exception, it will have to be in the throws clause, meaning another layer which calls it with either have to handle (catch) the exception or throw it further up the stack.

If the exception is unchecked however, a throws clause will not be required and even if it is stated, a calling method does not have to deal with it.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 541
thanks

If the try finally method throws a checked exception, it will have to be in the throws clause, meaning another layer which calls it with either have to handle (catch) the exception or throw it further up the stack.


So in this case is my below understanding correct?:

With this case of try finally there will surely be another layer having a try catch to handle it?


James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

So in this case is my below understanding correct?:

With this case of try finally there will surely be another layer having a try catch to handle it?


Another layer would need to either catch it or throw it.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1939
    
  28
Monica. Shiralkar wrote:With this case of try finally there will surely be another layer having a try catch to handle it?

For checked exceptions at some point in the call stack there will almost certainly be a catch statement to handle the exception but there doesn't have to be and for unchecked exceptions the likelihood is nothing will catch the exception.
If nothing handles the exception, a stack trace will be dumped to the console.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7052
    
  16

Monica. Shiralkar wrote:With this case of try finally there will surely be another layer having a try catch to handle it?

As James says: presumably. But then you need to ask yourself: do I really need try...finally?

Try blocks inhibit a lot of optimizations that the compiler and the JVM can do for you, and therefore tend to run slower. While this is no reason not to use them in the right situations, they simply add processing weight if you don't have any Exceptions or (as Paul said) resources to deal with.

Winston
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 541
thanks
Parvathi Raju
Greenhorn

Joined: Aug 04, 2013
Posts: 9
If you want to execute some statement whther an exception is occur or not or need to clean resources like closing stream we can use finally.Because finally method will execute always rather than System.exit(0).Finally block overrides the exception thrown by try block.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion on when to use Try-Finally combination instead of Try-catch or Try-catch-finally?
 
Similar Threads
Need for finally block?
try, catch and finally
help in exceptions
SCJP 1.4 K&B Book Chp 4 clarification reqd.
Little confusion in Finally block