Bug 23466 - Compiler violates JLS 8.3.2
Summary: Compiler violates JLS 8.3.2
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0.1   Edit
Hardware: PC Windows NT
: P3 normal (vote)
Target Milestone: 2.1 M1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-09-12 06:26 EDT by Kenny MacLeod CLA
Modified: 2002-09-23 11:22 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kenny MacLeod CLA 2002-09-12 06:26:57 EDT
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.
Comment 1 Philipe Mulet CLA 2002-09-12 10:07:04 EDT
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.
Comment 2 Philipe Mulet CLA 2002-09-13 11:53:40 EDT
Fixed.
Comment 3 Philipe Mulet CLA 2002-09-18 07:32:18 EDT
Reassigned
Comment 4 David Audel CLA 2002-09-19 07:36:10 EDT
Verified.
Comment 5 Kenny MacLeod CLA 2002-09-23 11:22:22 EDT
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.