wood burning stoves 2.0*
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
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Problem using finally" Watch "Problem using finally" New topic
Author

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
Marshal

Joined: Jul 08, 2003
Posts: 24185
    
  34

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: http://aspose.com/file-tools
 
subject: Problem using finally