Information Technology Reference
In-Depth Information
Searching Further
If the exception was raised in a section of code that was not guarded by a
try
statement, or if
the
try
statement did not have a matching exception handler—the system will have to look
further for a matching handler. It will do this by searching down the call stack, in sequence, to
see whether there is an enclosing
try
block with a matching handler.
Figure 11-7 illustrates the search process. On the left of the figure is the calling structure
of the code, and on the right is the call stack. The figure shows that
Method2
is called from inside
the
try
block of
Method1
. If an exception occurs inside the
try
block of
Method2
, the system per-
forms the following steps:
First, the system checks to see whether
Method2
was called from an enclosing
try
statement.
-If so,
Method2
's
catch
clauses are checked to see whether any of them match the
exception.
If not, the system continues down the call stack to
Method1
, searching for an
appropriate handler.
-
f
Method1
has an appropriate
catch
clause
-
The system goes back to the top of the call stack—which is
Method2
.
The system executes
Method2
's
finally
block, and pops
Method2
off the stack.
-
It then executes
Method1
's
catch
clause and its
finally
block.
-
f
Method1
does not have an appropriate
catch
clause, it will continue searching down
the call stack.
Figure 11-7.
Searching down the call stack