This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Finally block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Finally block" Watch "Finally block" New topic

Finally block

George Harris
Ranch Hand

Joined: May 05, 2003
Posts: 84
We all understand that the finally block will 'always' run regardless of the outcome of the try catch block. However there is one exception if System.exit(0) is called, the finally will not run. I was wondering if anyone knew if an 'out of memory' error would also cause the finally to be ignored. I have looked through the API to no avail.

Any guesses???
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
No, the finally block won't be ignored; it will execute. Be careful what you do in the finally block though, as the program could throw a new exception from the finally block. If memory is scarce already, then if you do something which attempts to use more memory, that could cause a new OOME (replacing the old one).

"I'm not back." - Bill Harding, Twister
Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

Why guess when you can test?
Don't get me started about those stupid light bulbs.
subject: Finally block
Similar Threads
When finally block does not run?
Error in Answer for Java Quiz
Roundup question #132
System.exit(0) &System.exit(1)