Java Reference
In-Depth Information
A
throw
statement first evaluates the
Expression
. Then:
• If evaluation of the
Expression
completes abruptly for some reason, then the
throw
completes abruptly for that reason.
• If evaluation of the
Expression
completes normally, producing a non-
null
value
V
,
then the
throw
statement completes abruptly, the reason being a
throw
with value
V
.
• If evaluation of the
Expression
completes normally, producing a
null
value, then an
instance
V'
of class
NullPointerException
is created and thrown instead of
null
. The
throw
statement then completes abruptly, the reason being a
throw
with value
V'
.
It can be seen, then, that a
throw
statement always completes abruptly.
ment, then any
finally
clauses of those
try
statements are executed as control is transferred
outward, until the thrown value is caught. Note that abrupt completion of a
finally
clause
can disrupt the transfer of control initiated by a
throw
statement.
If a
throw
statement is contained in a method declaration, but its value is not caught by some
try
statement that contains it, then the invocation of the method completes abruptly because
of the
throw
.
If a
throw
statement is contained in a constructor declaration, but its value is not caught by
some
try
statement that contains it, then the class instance creation expression that invoked
ways caught by some
try
statement that contains it. If at run time, despite this check, the
value is not caught by some
try
statement that contains the
throw
statement, then the value is
rethrown if it is an instance of class
Error
or one of its subclasses; otherwise, it is wrapped
ways caught by some
try
statement that contains it, or the type of the thrown exception (or
one of its superclasses) occurs in the
throws
clause of every constructor of the class.
By convention, user-declared throwable types should usually be declared to be sub-