aspose file tools*
The moose likes Beginning Java and the fly likes Trying to understand when to use 'finally' clause Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to understand when to use Watch "Trying to understand when to use New topic
Author

Trying to understand when to use 'finally' clause

Rudy Rusli
Ranch Hand

Joined: Jun 01, 2006
Posts: 114
I'm trying to understand the use of finally clause. I have these lines of codes below and it will produce the same result ("Done") whether I put the in the finally clause or not.

Thanks in advance for the help.
- Rudy -

-------------------------------------------------------

[ March 29, 2007: Message edited by: Rudy Rusli ]
Manhar Puri
Ranch Hand

Joined: Aug 23, 2005
Posts: 41
Rudy,

In the above case "Done" will not be printed if an exception occurs.

In case of finally no matter what happens in the try block the finally block of code is executed. You should put code like cleaning database connections in the finally block, since you always want to close the database connection no matter what!

Hope this helps.

-Manhar
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3658
    
  15
Originally posted by Manhar Puri:

In the above case "Done" will not be printed if an exception occurs.


Yes it will. The stacktrace will be printed and then the code will carry on to print the final message.

Rudy
They only both do the same because you are catching the exception and then allowing the code to continue.
Compare
and


Joanne
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Manhar Puri:
Rudy,

In the above case "Done" will not be printed if an exception occurs.


Yes it will. However it wont be printed if an Error is raised between the try and the catch.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Rudy Rusli:
I'm trying to understand the use of finally clause...

See the topic, What's finally for? from Thinking in Java.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rudy Rusli
Ranch Hand

Joined: Jun 01, 2006
Posts: 114
Paul,

Could you please explain more what do you mean by error between the try and catch?
In my code, there is an error (ArrayIndexOutOfBoundException) between the try and catch.

>>>>
Yes it will. However it wont be printed if an Error is raised between the try and the catch.
>>>>
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
ArrayIndexOutOfBoundsException is not an Error.

I think Paul was talking about things like OutOfMemoryError or another subclass of Error.
Rahul Shilpakar
Ranch Hand

Joined: Aug 29, 2006
Posts: 132
finally is used for releasing resources aquired by the threads so as there woldn't be any deadlock while fetching shared resource.


Perform for today. Adapt for tomorrow.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Keith Lynn:
ArrayIndexOutOfBoundsException is not an Error.

I think Paul was talking about things like OutOfMemoryError or another subclass of Error.


Yes. Remember Errors are not Exceptions, but will have simmilar effects. Consider this:

[ April 02, 2007: Message edited by: Paul Sturrock ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to understand when to use 'finally' clause