Concepts of Programming Languages

(Sean Pound) #1
14.4 Exception Handling in Java 653

try {

...
}
catch (.. .) {
...
}
... //** More handlers
finally {
...
}


The semantics of this construct is as follows: If the try clause throws no
exceptions, the finally clause is executed before execution continues after
the try construct. If the try clause throws an exception and it is caught by a
following handler, the finally clause is executed after the handler completes
its execution. If the try clause throws an exception but it is not caught by a
handler following the try construct, the finally clause is executed before
the exception is propagated.
A try construct with no exception handlers can be followed by a finally
clause. This makes sense, of course, only if the compound statement has a
throw, break, continue, or return statement. Its purpose in these cases
is the same as when it is used with exception handling. For example, consider
the following:

try {
for (index = 0; index < 100; index++) {

...
if (... ) {
return;
} // end of if
...
} //
end of for
} // end of try clause
finally {
...
} //
end of try construct


The finally clause here will be executed, regardless of whether the return
terminates the loop or it ends normally.

14.4.7 Assertions


In the discussion of Plankalkül in Chapter 2, we mentioned that it included
assertions. Assertions were added to Java in version 1.4. To use them, it is nec-
essary to enable them by running the program with the enableassertions
(or ea) flag, as in
Free download pdf