Java Reference
In-Depth Information
Software Engineering Observation 11.9
An exception can be thrown without containing information about the problem that
occurred. In this case, simply knowing that an exception of a particular type occurred may
provide sufficient information for the handler to process the problem correctly.
Software Engineering Observation 11.10
Throw exceptions from constructors to indicate that the constructor parameters are not
valid—this prevents an object from being created in an invalid state.
Rethrowing Exceptions
Line 32 of Fig. 11.5
rethrows the exception
. Exceptions are rethrown when a
catch
block,
upon receiving an exception, decides either that it cannot process that exception or that it
can only partially process it. Rethrowing an exception defers the exception handling (or
perhaps a portion of it) to another
catch
block associated with an outer
try
statement. An
exception is rethrown by using the
throw
keyword
, followed by a reference to the excep-
tion object that was just caught. Exceptions cannot be rethrown from a
finally
block, as
the exception parameter (a local variable) from the
catch
block no longer exists.
When a rethrow occurs, the
next enclosing
try
block
detects the rethrown exception,
and that
try
block's
catch
blocks attempt to handle it. In this case, the next enclosing
try
block is found at lines 8-11 in method
main
. Before the rethrown exception is handled,
however, the
finally
block (lines 37-40) executes. Then method
main
detects the
rethrown exception in the
try
block and handles it in the
catch
block (lines 12-15).
Next,
main
calls method
doesNotThrowException
(line 17). No exception is thrown
in
doesNotThrowException
's
try
block (lines 49-52), so the program skips the
catch
block (lines 53-56), but the
finally
block (lines 57-61) nevertheless executes. Control
proceeds to the statement after the
finally
block (line 63). Then control returns to
main
and the program terminates.
Common Programming Error 11.4
If an exception has not been caught when control enters a
finally
block and the
finally
block throws an exception that's not caught in the
finally
block,
the first exception will
be lost
and the exception from the
finally
block will be returned to the calling method.
Error-Prevention Tip 11.6
Avoid placing in a
finally
block code that can
throw
an exception. If such code is re-
quired, enclose the code in a
try
…
catch
within the
finally
block.
Common Programming Error 11.5
Assuming that an exception thrown from a
catch
block will be processed by that
catch
block
or any other
catch
block associated with the same
try
statement can lead to logic errors.
Good Programming Practice 11.1
Exception handling removes error-processing code from the main line of a program's code to
improve program clarity. Do not place
try
…
catch
…
finally
around every statement that
may throw an exception. This decreases readability. Rather, place one
try
block around a
significant portion of your code, follow the
try
with
catch
blocks that handle each possible
exception and follow the
catch
blocks with a single
finally
block (if one is required).