If the code below is run from an IDE or from the command line, and then stopped either:
- Stopping its execution from eclipse (red button)
- Pressing CTRL+C (command line)
- Naturally (by replacing the while(true) loop by a for loop
never the finally clause is executed ("Finally" never printed out).
public class Test {
public static void main(String[] args) {
try {
while (true) {
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("Finally");
}
}
}
Quoting the Java 7 specifications:
If execution of the try block completes normally, then the finally block is executed, and then there is a choice:
Any insights?
EDIT
Oops, my bad, a finite for loop cause the finally clause to be called. I haven't noticed the trace in my unit test. So their is no question now.