Community
Participate
Working Groups
According to JLS 8.3.2: "It is a compile-time error if the evaluation of a variable initializer for a static field or for an instance variable of a named class (or of an interface) can complete abruptly with a checked exception." Take the following code sample: class X { Y y = new Y(); X() throws Exception { System.out.println( "Constructing X" ); } } class Y { Y() throws Exception { System.out.println( "Constructing Y" ); throw new Exception(); } } The default Eclipse compiler lets this pass OK, but jikes complains with this: 11. Y y = new Y(); ^ *** Error: The constructor "Y" can throw the checked exception "java/lang/Exception", but its invocation is neither enclosed in a try statement that can catch that exception nor in the body of a method or constructor that "throws" that exception. JDK 1.3.1 javac also complains. Note that if you remove the constructor definition from X, then Eclipse fails to compile. This seems to imply that Eclipse is assuming that the exception thrown from the instance field can be thrown back out from X's constructor, which violates the JLS. Also, if you write a test program which attempts to construct an instance of X using the above code, then the output is as follows: Constructing Y Exception in thread "main" java.lang.Exception at Y.<init> at X.<init> So, X's constructor is never being called. The stack trace indicates that the instance field of X is being executed before the constructor, which is correct.
We currently do behave like javac 1.4, but I think they have a bug too. We should only tolerate exception to be thrown from within instance initializers, not variable initializers, as per JLS 8.6.
Fixed.
Reassigned
Verified.
Can this bug fix be moved into the 2.0.2 release, rather than the indicated 2.1 M1? It's causing us significant problems at the moment.