-
Bug
-
Resolution: Fixed
-
P4
-
6
According to JLS3 14.20, a catch block for a checked exception type should cause a compile-time error if the corresponding try block cannot throw (according to the rules of 11.2, presumably) that exception type or a subtype of it (i.e. if the catch block is essentially pointless). In the "T1" example below, however, it appears that the try block's "can throw" determination is incorrect-- perhaps it is getting confused by code in an enclosed anonymous class declaration?
When T2.java is compiled (using Mustang B69's javac):
import java.security.*;
public class T2 {
public static void main(String[] args) throws ClassNotFoundException {
final String name = args[0];
try {
AccessController.doPrivileged(new PEA(name));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (PrivilegedActionException e) {
e.printStackTrace();
}
}
private static class PEA implements PrivilegedExceptionAction {
private final String name;
PEA(String name) { this.name = name; }
public Object run() throws ClassNotFoundException {
return Class.forName(name);
}
}
}
it fails as expected with this error:
T2.java:7: exception java.lang.ClassNotFoundException is never thrown in body of corresponding try statement
} catch (ClassNotFoundException e) {
^
1 error
but T1.java, which changes the PrivilegedExceptionAction implementation from a member class to an anonymous class, compiles without error:
import java.security.*;
public class T1 {
public static void main(String[] args) throws ClassNotFoundException {
final String name = args[0];
try {
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws ClassNotFoundException {
return Class.forName(name);
}
});
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (PrivilegedActionException e) {
e.printStackTrace();
}
}
}
even though the try block "cannot throw" ClassNotFoundException, making the catch-CNFE block almost surely not serve the purpose that the author intended.
T1.java also compiles without error using the javac from 5.0 and 1.4.2, but the expected error is reported when the 1.3.1 compiler is used (either javac or oldjavac, actually).
When T2.java is compiled (using Mustang B69's javac):
import java.security.*;
public class T2 {
public static void main(String[] args) throws ClassNotFoundException {
final String name = args[0];
try {
AccessController.doPrivileged(new PEA(name));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (PrivilegedActionException e) {
e.printStackTrace();
}
}
private static class PEA implements PrivilegedExceptionAction {
private final String name;
PEA(String name) { this.name = name; }
public Object run() throws ClassNotFoundException {
return Class.forName(name);
}
}
}
it fails as expected with this error:
T2.java:7: exception java.lang.ClassNotFoundException is never thrown in body of corresponding try statement
} catch (ClassNotFoundException e) {
^
1 error
but T1.java, which changes the PrivilegedExceptionAction implementation from a member class to an anonymous class, compiles without error:
import java.security.*;
public class T1 {
public static void main(String[] args) throws ClassNotFoundException {
final String name = args[0];
try {
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws ClassNotFoundException {
return Class.forName(name);
}
});
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (PrivilegedActionException e) {
e.printStackTrace();
}
}
}
even though the try block "cannot throw" ClassNotFoundException, making the catch-CNFE block almost surely not serve the purpose that the author intended.
T1.java also compiles without error using the javac from 5.0 and 1.4.2, but the expected error is reported when the 1.3.1 compiler is used (either javac or oldjavac, actually).
- relates to
-
JDK-6557881 Incorrect code fixed incorrectly in compiler bug fix 6379327
- Closed