The finally clause executes wheter or not an exception is thrown. Except when a catch clause invokes System.exit() of course!
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
Taken from the Java in a Nutshell
try {
// Normally this code runs from the top of the block to the bottom
// without problems. But it can sometimes throw and exception,
// either directly or with a throw statement or intdirectly by calling
// a method that throws and exception.
}
catch (SomeException e1) {
// This block contains statements that handle the exception object
// of type SomeException or a subclass of that type. Statements in
// this block can refer to that exception object by the name e1.
}
catch (AnotherException e2) {
// This block contains statements that handle the exception object
// of type AnotherException or a subclass of that type. Statements in
// this block can refer to that exception object by the name e2.
}
finally {
// This block contains statements that are ALWAYS executed
// after leaving the try clause, regardless of whether we leave it:
// 1) normally after reaching the bottom of the block;
// 2) because of a break, continue, or return statement;
// 3) with an exception handled by a catch clause above; or
// 4) with an uncaught exception that has not been handled.
// If the try clause calls System.exit(), however, the interpreter
// exits before the finally clause can be run.
}
Also, later on in the finally description of the document:
If control leaves the try block because of a return, continue, or break statement, the finally block is executed before control transfers to its new destination.
This would also apply to if it is used in the catch block.
Hope this clarifies things for you.
jerbo
Junior Poster in Training
84 posts since Sep 2004
Reputation Points: 11
Solved Threads: 1