• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When finally block does not run?

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When finally block does not run?(try/catch)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Java Tutorials - The finally block.
 
Brad Dwan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brad Dwan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When finally block does not run?(try/catch)

When the try or catch blocks call System.exit()
EDIT: Oops, this was already said.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic