Information Technology Reference
In-Depth Information
This code produces the following output:
finally clause in B()
finally clause in A()
catch clause in Main()
finally clause in Main()
After try statement in Main.
-- Keep running.
1.
Main
calls
A
, which calls
B
, which encounters a
DivideByZeroException
exception.
2.
The system checks
B
's
catch
section for a matching
catch
clause. Although it has one for
IndexOutOfRangeException
, it does not have one for
DivideByZeroException
.
The system then moves down the call stack and checks
A
's
catch
section, where it finds
that
A
also does not have a matching
catch
clause.
3.
The system continues down the call stack, and checks
Main
's
catch
clause section,
where it finds that
Main
does
have a
DivideByZeroException
catch
clause.
4.
Although the matching
catch
clause has now been located, it is not executed yet.
Instead, the system goes back to the top of the stack, executes
B
's
finally
clause, and
pops
B
from the call stack.
5.
The system then moves to
A
, executes its
finally
clause, and pops
A
from the call stack.
6.
7.
Finally,
Main
's matching
catch
clause is executed, followed by its
finally
clause. Execu-
tion then continues after the end of
Main
's
try
statement.