bouncingHippo
bouncingHippo

Reputation: 6040

Will finally clause always always execute?

try {
    if (myBoolean) {
        while (true) ;
    } else {
        System.exit(1);
    }
} finally {
    code.to.cleanup();
}

I am not entirely sure, but here's the above code snippet that i feel may cause the finally clause to not execute, regardless of myBoolean's value

Upvotes: 2

Views: 671

Answers (5)

Ben
Ben

Reputation: 71495

Just a note, since others have answered the specifics perfectly well already: finally is essentially a guarantee that if execution leaves the try block no other code will run before the finally block is run. It does not guarantee that the finally block will be run. The latter would be impossible; consider a blackout or a hardware failure, in the extreme case.

finally is intended to enable you to provide guarantees to the rest of your program about the state of things after execution leaves the try/finally block. finally is not intended to provide guarantees to the world outside your program. In cases where "the rest of your program" doesn't continue to run, it's more the responsibility of the OS to clean up after the dead process in terms of resources it has handed out, and the responsibility of other systems interacting with your program to have ways of handling the sudden failure of your process.

Upvotes: 0

user85
user85

Reputation: 1596

In your example, finally won't be executed as the code in if block is an infinite loop and in else block it causes the JVM to shutdown. But for any other normal scenario finally will always be executed even if you have return keyword in your try block.

try {
  System.out.println("Returning from here....");
  return;
} finally {
  System.out.println("Inside finally....");
}

Output will be:

Returning from here....
Inside finally....

Upvotes: 0

Yair Zaslavsky
Yair Zaslavsky

Reputation: 4137

System.exit(1) performs a brutal exit of the application, ignoring anything that is on the call stack, including the finally block.
Bare in mind, that GC will not run as well, so resources that are used by the application might left in a "hung" or "zombie" state (files that are not closed, etc..)
If this is a question for just the sake of theory , ignore my comment.

Upvotes: 0

Chandra Sekhar
Chandra Sekhar

Reputation: 19502

Because if myBoolean value is true, it will enter into an infinite loop. i think you have misplaced a semicolon after while statement.

In 2nd case, if myBoolean is false, the System.exit(1) makes the program to exit without running the any further statements.

Upvotes: 2

aioobe
aioobe

Reputation: 421040

No, System.exit(1) prevents the finally clause from running.

Basically a finally block is executed after a try/catch regardless if the try or the catch returned normally or exceptionally.

A System.exit however prevents the block from returning at all.

(As Peter points out however, while(true) ; will obviously block indefinitely. A just assumed that the while (true) ; was a stub for something that made more sense :)

Upvotes: 9

Related Questions