Java Reference
In-Depth Information
It can be seen, then, that a return statement always completes abruptly.
The preceding descriptions say “attempts to transfer control” rather than just “trans-
fers control” because if there are any try statements (§ 14.20 ) within the method or con-
structor whose try blocks or catch clauses contain the return statement, then any finally
clauses of those try statements will be executed, in order, innermost to outermost, be-
fore control is transferred to the invoker of the method or constructor. Abrupt comple-
tion of a finally clause can disrupt the transfer of control initiated by a return statement.
14.18. The throw Statement
A throw statement causes an exception (§11) to be thrown. The result is an immediate trans-
fer of control (§ 11.3 ) that may exit multiple statements and multiple constructor, instance
initializer, static initializer and field initializer evaluations, and method invocations until
a try statement (§ 14.20 ) is found that catches the thrown value. If no such try statement is
found, then execution of the thread (§17) that executed the throw is terminated (§ 11.3 ) after
invocation of the uncaughtException method for the thread group to which the thread belongs.
ThrowStatement:
throw Expression ;
The Expression in a throw statement must denote either 1) a variable or value of a reference
type which is assignable (§ 5.2 ) to the type Throwable , or 2) the null reference, or a compile-
time error occurs.
The reference type of the Expression will always be a class type (since no interface
types are assignable to Throwable ) which is not parameterized (since a subclass of
Throwable cannot be generic (§ 8.1.2 ) ).
At least one of the following three conditions must be true, or a compile-time error occurs:
• The type of the Expression is an unchecked exception class (§ 11.1.1 ) or the null
type (§ 4.1 ).
• The throw statement is contained in the try block of a try statement (§ 14.20 ) and it is
not the case that the try statement can throw an exception of the type of the Expres-
sion . (In this case we say the thrown value is caught by the try statement.)
• The throw statement is contained in a method or constructor declaration and the
type of the Expression is assignable (§ 5.2 ) to at least one type listed in the throws
clause (§ 8.4.6 , § 8.8.5 ) of the declaration.
The exception types that a throw statement can throw are specified in § 11.2.2 .
Search WWH ::




Custom Search