0

Possible Duplicate:
Is there such case when in try\finally block the finally won't be executed?

I'm relatively new to java and I've just heard about try-catch blocks (3 minutes ago to be exact).

In a try-catch block, is the finally part (when present) ever not executed?

try {
    if (choice) {
        //...
    } else {
        //...
    }
} finally {
    doesThisEverNOTrun();
}
Community
  • 1
  • 1
Tony
  • 419
  • 1
  • 7
  • 8
  • 4
    possible duplicate of [Is there such case when in try\finally block the finally won't be executed?](http://stackoverflow.com/questions/3484353/is-there-such-case-when-in-try-finally-block-the-finally-wont-be-executed) and http://stackoverflow.com/questions/6228528/ and http://stackoverflow.com/questions/464098/ – Matt Ball Aug 20 '11 at 13:36
  • [Yes](http://thedailywtf.com/Articles/My-Tales.aspx). ;) – Adam Paynter Aug 20 '11 at 13:43

2 Answers2

0

Yes.

try {
    if (choice) {
        while (true);
    } else {
        System.exit(0);
    }
} finally {
    doesThisEverNotRun(YES);
}

More often than not, though, the finally part is executed.

fireshadow52
  • 6,298
  • 2
  • 30
  • 46
0

If the (virtual) machine does not crash, you don't call System.exit() in the try or a catch block, the finally block will execute.

Nudelsuppe
  • 157
  • 1
  • 12