• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1283
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also note that finally block is executed after keywords break, continue, return. Finally is executed regardless if exception was caught or not!
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic