-
Bug
-
Resolution: Fixed
-
P4
-
1.1.2, 1.1.4, 1.2.0, 1.3.0, 1.4.2, 5.0, 7
-
Verified
javac doesn't comply with the JLS on the handling of unreachable statements in the context of try blocks and unchecked exceptions. The following is specified to be an error:
class Test {
void f() {
try {
} catch (Error ex) {
; // unreachable
}
}
}
Without the semicolon, it is legal! (because there is no unreachable statement).
These problems are probably spec issues. I would prefer to see the following changes to the spec:
(1) Add "The Block of a catch block is reachable iff the catch block is reachable".
That addresses the difference in behavior on introduction of the semicolon. Also, I'd like to see this
# 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.
changed to this
# A catch block C is reachable iff both of the following are true:
* Either
# 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.),
or
# the type of C's parameter is an unchecked exception type.
* 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.
That gives the compiler permission to consider any unchecked exception to be thrown in the try block, even if the try block is empty.
class Test {
void f() {
try {
} catch (Error ex) {
; // unreachable
}
}
}
Without the semicolon, it is legal! (because there is no unreachable statement).
These problems are probably spec issues. I would prefer to see the following changes to the spec:
(1) Add "The Block of a catch block is reachable iff the catch block is reachable".
That addresses the difference in behavior on introduction of the semicolon. Also, I'd like to see this
# 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.
changed to this
# A catch block C is reachable iff both of the following are true:
* Either
# 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.),
or
# the type of C's parameter is an unchecked exception type.
* 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.
That gives the compiler permission to consider any unchecked exception to be thrown in the try block, even if the try block is empty.
- duplicates
-
JDK-4046575 JLS3 14.21 Spec for reachability doesn't consider unchecked exceptions
-
- Closed
-
-
JDK-4994035 Unreachable Catch Block Not Flagged
-
- Closed
-