The Java Tutorials' Weblog

pageicon Thursday Aug 28, 2008

The finally Block Always Executes

We recently received some very good feedback from a reader, Russel Taylor, regarding The finally Block page.

Russel pointed out that, though it might be obvious, the finally block will not be executed if the JVM exits while in the process of executing the try or catch code. Likewise (and this is where it might be less obvious), if the code is executing on another thread and that thread is interrupted or killed, the finally block would not be executed, even if the application continues.

This exact problem led to a production outage that Russel helped to debug.

I have modified this page of the tutorial to include a note about this fact. Thanks, Russel, for helping us to improve the tutorial!

-- Sharon Zakhour

Comments:

Keep in mind also that a finally block will not be executed if there is a power outage.

Check this funny story about finally blocks: http://thedailywtf.com/Articles/My-Tales.aspx

Posted by Leonel on August 28, 2008 at 12:54 PM PDT #

[Trackback] the finally block would not be executed, even if the application continues.

Posted by rath's me2DAY on August 28, 2008 at 07:06 PM PDT #

That is an excellent anecdote, Leonel!

Posted by Sharon Zakhour on August 29, 2008 at 12:54 PM PDT #

The best, most concise and explicitly clear explanation I have ever seen.

Thanks

Posted by Hurray_finally on June 14, 2009 at 04:06 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today

Feeds

Search this blog

Links

Weblog menu

Today's referrers

Today's Page Hits: 452