Java Reference
In-Depth Information
know the exceptions potentially thrown in the try block, we call
atch
(Figure 9.27) to verify that some exception can reach each of the catch clauses in
the try. Finally, we call
process
C
(Figure 9.27) to determine the exceptions
that can ”escape” from the current try body (an exception need not be handled
locally).
Reachability visitors for try statements are defined in Figure 9.29. The
try body, finally statements, and all catch bodies are marked as reachable
(
filter
C
atches
helped verify this). A try terminates normally if the try body
or any catch clause can terminate normally and the finally statements also
terminate normally.
The AST for a throw statement is shown in Figure 9.30. Semantic analysis
(Figure 9.26, Marker 31 ) verifies that the type of value thrown is a valid
exception (a type assignable to Throwable).
Throws analysis (Figure 9.28, Marker 34 ) verifies that if a checked ex-
ception is thrown, an enclosing try block can catch the exception or the en-
closing method or constructor has included the exception in its throws list.
We ca l l
process
C
atch
to get the exceptions mentioned in all enclosing try
blocks. The exceptions in the current method's throws list is obtained by call-
ing
get
C
atch
L
ist
hrows
to remove exceptions that canmatch the thrown exception. If no exceptions are
removed, the thrown exception is not properly handled and an error message
is issued.
As an example, consider the following Java code fragment:
get
D
ecl
T
hrows
L
ist
. We join these two lists together and call
filter
T
class ExitComputation extends Exception{};
try { ...
if (cond)
throw new ExitComputation();
if (v < 0.0)
throw new ArithmeticException();
else a = Math.sqrt(v);
... }
catch (e ExitComputation) {return 0;}
A new checked exception, ExitComputation, is declared. In the try statement,
we first check the catch clause. The current catch list is extended with an entry
for type ExitComputation. The try body is then checked. Focusing on throw
statements, we first process a throw of an ExitComputation object. This is
a valid subclass of Throwable,andExitComputation is on the current catch
list, so no errors are detected. Next, the throw of an ArithmeticException
is checked. It too is a valid exception type. It is an unchecked exception (a
 
Search WWH ::




Custom Search