Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem using finally

 
Haroldo Nascimento
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"finally" and "finalizers" are not the same thing at all.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that a call to System.exit() in the try block will cause the program to halt without the finally part being executed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic