Java Reference
In-Depth Information
... body ...
} catch (final IllegalAccessException ex2) {
... body ...
}
whereby the multi- catch clause with two alternatives has been translated into two sep-
arate catch clauses, one for each alternative. A Java compiler is neither required nor re-
commended to compile a multi- catch clause by duplicating code in this manner, since
it is possible to represent the multi- catch clause in a class file without duplication.
A finally clause ensures that the finally block is executed after the try block and any catch
block that might be executed, no matter how control leaves the try block or catch block.
Handling of the finally block is rather complex, so the two cases of a try statement with and
without a finally block are described separately (§ 14.20.1 , § 14.20.2 ) .
A try statement is permitted to omit catch clauses and a finally clause if it is a try -with-re-
sources statement (§ 14.20.3 ).
14.20.1. Execution of try - catch
A try statement without a finally block is executed by first executing the try block. Then there
is a choice:
• If execution of the try block completes normally, then no further action is taken and
the try statement completes normally.
• If execution of the try block completes abruptly because of a throw of a value V ,
then there is a choice:
♦ If the run-time type of V is assignment compatible with (§ 5.2 ) a catchable ex-
ception class of any catch clause of the try statement, then the first (leftmost)
such catch clause is selected. The value V is assigned to the parameter of the se-
lected catch clause, and the Block of that catch clause is executed, and then there
is a choice:
If that block completes normally, then the try statement completes nor-
mally.
If that block completes abruptly for any reason, then the try statement
completes abruptly for the same reason.
♦ If the run-time type of V is not assignment compatible with a catchable excep-
tion class of any catch clause of the try statement, then the try statement com-
pletes abruptly because of a throw of the value V .
Search WWH ::




Custom Search