United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 4054256 instance initializers should be able to throw exceptions
4054256 : instance initializers should be able to throw exceptions

Details
Type:
Bug
Submit Date:
1997-05-23
Status:
Closed
Updated Date:
2001-10-18
Project Name:
JDK
Resolved Date:
2000-05-05
Component:
tools
OS:
solaris_2.5,solaris_2.5.1,generic,windows_95
Sub-Component:
javac
CPU:
x86,sparc,generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.1,1.1.1,1.2.0
Fixed Versions:
1.4.0

Related Reports
Duplicate:
Duplicate:
Duplicate:

Sub Tasks

Description

Name: mc57594			Date: 05/23/97


An instance initializer is allowed to throw a checked
exception if that exception type is declared in the throws list of every
constructor.  Thus the following code is legal:

class B 
{
	class C 
	{
		C () throws Throwable 
		{
		}
            
		{
		int j = 5;
		if (j == 6)
		throw new Throwable(); // thrown by constructor
		}
	}
}

However javac rejects this.

Also note that initializers in anonymous classes may throw ANY checked
exception type.
======================================================================

Copied from 4030341, closed as duplicate, by william.maddox@Eng 1998-01-27:

The inner classes white paper says that a checked exception may be raised
in an instance initializer if all constructors declare a throw for it,
and that the automatically generated constructor for an anonymous class
automatically throws the correct exceptions, so that any exception can be
raised in the body of an anonymous class.

The compiler produces several errors if this is attempted.  Here is an example:
	// this is JCK 1.1 test innr088
	class InitExceptionBug0 {
	    void f() throws InstantiationException {
		new Object() {
		    {
			throw new InstantiationException("test");
		    }
		};
	    }
	}

The compiler output looks like this:

InitExceptionBug0.java:4: Statement not reached.
	new Object() {
	    ^
InitExceptionBug0.java:4: Constructor invocation must be the first thing in a method.
	new Object() {
	    ^
InitExceptionBug0.java:6: Exception java.lang.InstantiationException can't be thrown in initializer.
		throw new InstantiationException("test");
		^
3 errors




                                    

Comments
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
generic
merlin

FIXED IN:
merlin
merlin-beta

INTEGRATED IN:
merlin

VERIFIED IN:
merlin-beta3


                                     
2004-06-14
WORK AROUND



Name: mc57594			Date: 05/23/97


No.
======================================================================
                                     
2004-06-11
EVALUATION

Verified behavior under 1.2alpha.
Roly's analysis sounds right.  See also 4030341.

david.stoutamire@Eng 1997-05-29

See also 4102541, which I suspect has the same underlying cause.

william.maddox@Eng 1998-01-27
                                     
1997-05-29



Hardware and Software, Engineered to Work Together