File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Problem using finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Problem using finally" Watch "Problem using finally" New topic

Problem using finally

Haroldo Nascimento
Ranch Hand

Joined: Aug 14, 2003
Posts: 79
There is any problem in using finally?
I read that the programer should never depend on a finalizer to update critical persistent state, because the JLS provide no guarantee that finalizers will get executed promptly, it provides no guarantee that a program terminates without executing finalizers on some objects that are no longer reachable.
It is true ??
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
"finally" and "finalizers" are not the same thing at all.

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

To agree with and expand on what Ron said, yes, you can't absolutely depend on the finalize() method ever being called for any object. You can, however, depend on finally blocks being invoked precisely when they're supposed to be. In fact, the correct alternative to depending on finalize() is to use finally {} instead.

[Jess in Action][AskingGoodQuestions]
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Note that a call to System.exit() in the try block will cause the program to halt without the finally part being executed.
I agree. Here's the link:
subject: Problem using finally
jQuery in Action, 3rd edition