Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to understand when to use 'finally' clause

 
Rudy Rusli
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • 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 ]
 
Manhar Puri
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.
 
Rudy Rusli
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • 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.
>>>>
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayIndexOutOfBoundsException is not an Error.

I think Paul was talking about things like OutOfMemoryError or another subclass of Error.
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • 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
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic