A catch block C is reachable iff(if and only if) both of the following are true:
Some expression or throw statement in the try block is reachable and can throw an exception whose type is assignable to the parameter of the catch clause C. (An expression is considered reachable iff the innermost statement containing it is reachable.)
There is no earlier catch block A in the try statement such that the type of C's parameter is the same as or a subclass of the type of A's parameter.
Originally posted by James Du:
Let me try explain it in another way, if the statement could be regarded acceptable by the complier(i.e means reachable here), it must conform to either condition in the specification.
origally stated by jeena
java does not allow check exceptions to be handled if they are never thrown by any expressions in the try block.
Here my puzzle:
Does it mean that unreachable statement could be acceptable in
some special cases(e.g. the unreachable statement in the catch
clause of which the exception parameter is of subclass of
RuntimeException)?
Or that an empty block(i.e {}) is considered a potential
RuntimeException thrower? in this case, the statement enclosed in
the catch clause is not considered unreachable.
- The try block is reachable iff the try statement is reachable.
- A catch block C is reachable iff both of the following are
true:
- Some expression or throw statement in the try block is
reachable and can throw an exception whose type is assignable to
the parameter of the catch clause C. (An expression is considered
reachable iff the innermost statement containing it is
reachable.)
- There is no earlier catch block A in the try statement such
that the type of C's parameter is the same as or a subclass of
the type of A's parameter.
Whether a particular catch clause handles an exception is determined by comparing the class of the object that was thrown to the declared type of the parameter of the catch clause. The catch clause handles the exception if the type of its parameter is the class of the exception or a superclass of the class of the exception. Equivalently, a catch clause will catch any exception object that is an instanceof the declared parameter type.
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
originally posted by Jane
Theoretically, an empty try block, or any section of code can throw a RuntimeException.
I was her plaything! And so was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|