try
statementA try
statement executes a block. If a value is thrown and the try
statement has one or more catch
clauses that can catch it, then control will be transferred to the first such catch
clause. If the try
statement has a finally
clause, then another block of code is executed, no matter whether the try
block completes normally or abruptly, and no matter whether a catch
clause is first given control.
TryStatement:
try
BlockCatches
try
BlockCatchesopt
Finally Catches:
CatchClause
CatchesCatchClause CatchClause:
catch (
FormalParameter)
Block Finally:
finally
Block
The following is repeated to make the presentation here clearer:
FormalParameter:
TypeVariableDeclaratorId
The following is repeated to make the presentation here clearer:
VariableDeclaratorId:
Identifier
VariableDeclaratorId[ ]
The Block immediately after the keyword try
is called the try
block of the try
statement. The Block immediately after the keyword finally
is called the finally
block of the try
statement.
A try
statement may have catch
clauses (also called exception handlers). A catch
clause must have exactly one parameter (which is called an exception parameter); the declared type of the exception parameter must be the class Throwable
or a subclass of Throwable
, or a compile-time error occurs. The scope of the parameter variable is the Block of the catch
clause. An exception parameter must not have the same name as a local variable or parameter in whose scope it is declared, or a compile-time error occurs.
The scope of the name of an exception parameter is the Block of the catch
clause. The name of the parameter may not be redeclared as a local variable or exception parameter within the Block of the catch
clause; that is, hiding the name of an exception parameter is not permitted.
Exception parameters cannot be referred to using qualified names, only by simple names.
Exception handlers are considered in left-to-right order: the earliest possible catch
clause accepts the exception, receiving as its actual argument the thrown exception object.
A finally
clause ensures that the finally
block is executed after the try
block and any catch
block that might be executed, no matter how control leaves the try
block or catch
block.
Handling of the finally
block is rather complex, so the two cases of a try
statement with and without a finally
block are described separately.
try-catch
A try
statement without a finally
block is executed by first executing the try
block. Then there is a choice:
try
block completes normally, then no further action is taken and the try
statement completes normally.try
block completes abruptly because of a throw
of a value V, then there is a choice:
catch
clause of the try
statement, then the first (leftmost) such catch
clause is selected. The value V is assigned to the parameter of the selected catch
clause, and the Block of that catch
clause is executed. If that block completes normally, then the try
statement completes normally; if that block completes abruptly for any reason, then the try
statement completes abruptly for the same reason.catch
clause of the try
statement, then the try
statement completes abruptly because of a throw
of the value V.try
block completes abruptly for any other reason, then the try
statement completes abruptly for the same reason.In the example:
class BlewIt extends Exception { BlewIt() { } BlewIt(String s) { super(s); } } class Test { static void blowUp() throws BlewIt { throw new BlewIt(); } public static void main(String[] args) {
try { blowUp(); } catch (RuntimeException r) { System.out.println("RuntimeException:" + r); } catch (BlewIt b) { System.out.println("BlewIt"); } }
}
the exception BlewIt
is thrown by the method blowUp
. The try
-catch
statement in the body of main
has two catch
clauses. The run-time type of the exception is BlewIt
which is not assignable to a variable of type RuntimeException
, but is assignable to a variable of type BlewIt
, so the output of the example is:
BlewIt
try-catch-finally
A try
statement with a finally
block is executed by first executing the try
block. Then there is a choice:
try
block completes normally, then the finally
block is executed, and then there is a choice:
finally
block completes normally, then the try
statement completes normally.finally
block completes abruptly for reason S, then the try
statement completes abruptly for reason S.try
block completes abruptly because of a throw
of a value V, then there is a choice:
catch
clause of the try
statement, then the first (leftmost) such catch
clause is selected. The value V is assigned to the parameter of the selected catch
clause, and the Block of that catch
clause is executed. Then there is a choice:
catch
block completes normally, then the finally
block is executed. Then there is a choice:
finally
block completes normally, then the try
statement completes normally.finally
block completes abruptly for any reason, then the try
statement completes abruptly for the same reason.catch
block completes abruptly for reason R, then the finally
block is executed. Then there is a choice:
catch
clause of the try
statement, then the finally
block is executed. Then there is a choice:
try
block completes abruptly for any other reason R, then the finally
block is executed. Then there is a choice:
The example:
class BlewIt extends Exception { BlewIt() { } BlewIt(String s) { super(s); } }
class Test { static void blowUp() throws BlewIt {
throw new NullPointerException();
} public static void main(String[] args) { try { blowUp(); } catch (BlewIt b) { System.out.println("BlewIt"); } finally { System.out.println("Uncaught Exception"); } }
}
produces the output:
Uncaught Exception java.lang.NullPointerException at Test.blowUp(Test.java:7) at Test.main(Test.java:11)
The NullPointerException
(which is a kind of RuntimeException
) that is thrown by method blowUp
is not caught by the try
statement in main
, because a NullPointerException
is not assignable to a variable of type BlewIt
. This causes the finally
clause to execute, after which the thread executing main
, which is the only thread of the test program, terminates because of an uncaught exception, which results in printing the exception name and a simple backtrace.