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

Trying to understand when to use 'finally' clause

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Smith Li
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
>>>>
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayIndexOutOfBoundsException is not an Error.

I think Paul was talking about things like OutOfMemoryError or another subclass of Error.
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finally is used for releasing resources aquired by the threads so as there woldn't be any deadlock while fetching shared resource.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic