wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes When finally block does not run? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When finally block does not run?" Watch "When finally block does not run?" New topic
Author

When finally block does not run?

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
When finally block does not run?(try/catch)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See Java Tutorials - The finally block.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Brad Dwan
Ranch Hand

Joined: Apr 22, 2010
Posts: 62
Generally the finally block shouldn't be used except for "safety-net" situations. You should never rely on them to do their job for there is no guarantee that they will execute.


-Nev
"It's about choosing your battles, not fighting somebody else's war"
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Brad Dwan wrote:Generally the finally block shouldn't be used except for "safety-net" situations. You should never rely on them to do their job for there is no guarantee that they will execute.


Hi Brad,

I think you're thinking of overriding the finalize() method, which is, as you say, not guaranteed to be executed. finally blocks, on the other hand, will always be executed after the corresponding try, with the single exception being that the finally block won't execute if the JVM exits (via System.exit() or a crash) during the try. That's an iron-clad guarantee.


[Jess in Action][AskingGoodQuestions]
Brad Dwan
Ranch Hand

Joined: Apr 22, 2010
Posts: 62
Ernest Friedman-Hill wrote:
Brad Dwan wrote:Generally the finally block shouldn't be used except for "safety-net" situations. You should never rely on them to do their job for there is no guarantee that they will execute.


Hi Brad,

I think you're thinking of overriding the finalize() method, which is, as you say, not guaranteed to be executed. finally blocks, on the other hand, will always be executed after the corresponding try, with the single exception being that the finally block won't execute if the JVM exits (via System.exit() or a crash) during the try. That's an iron-clad guarantee.


My mistake. Noted. I can change a little of my own code then. :P
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
When finally block does not run?(try/catch)

When the try or catch blocks call System.exit()
EDIT: Oops, this was already said.
 
 
subject: When finally block does not run?