hi.

more or less a stupid question, but anyway..

why have a 'finally' statement in java exception handling -why not just clean up in the 'catch' clause??

is it for when an exception cannot be caught?? if not, then 'finally' seems a little redundant to me..

cheers.

:eek:

The finally clause executes wheter or not an exception is thrown. Except when a catch clause invokes System.exit() of course!

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 and Server_Crash,

that's great -thanks for your help.

It seems Java in a Nutshell is a good resource to have..

Cheers,
Less-confused ..
:D

This question has already been answered. Start a new discussion instead.