-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.1.7, 1.4.0, 6
-
generic, x86
-
generic, windows_2000, windows_xp
Name: krT82822 Date: 04/26/99
According to the Java language specification, a method may only
throw RuntimeExceptions, Errors, and 'checked' Exceptions
that are declared in its 'throws' clause.
The newInstance() method of the class java.lang.Class declares
two checked exceptions, namely InstantiationException and
IllegalAccessException, but it will actually throw any checked
Exception that might arise from invoking the default constructor
of the associated class.
This contravenes the language spec.
Example code:
public class Ex
{
public Ex() throws NoSuchFieldException
{
throw new NoSuchFieldException("error in constructor");
}
public static void main(String[] args)
{
try
{
Class.forName("Ex").newInstance();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Sample output:
java.lang.NoSuchFieldException: error in constructor
at Ex.<init>(Ex.java:5)
at Ex.main(Ex.java:12)
Java versions exhibiting this error include (but may not be
limited to):
JDK 1.1.7, Win32
JDK 1.1.7, Linux (Blackdown)
JDK 1.2, Linux (Blackdown)
plus Microsoft's JView.
(this bug also mentioned in David Holmes' comment on bug 4039848)
(Review ID: 57323)
======================================================================
- duplicates
-
JDK-4814544 (reflect) Class.newInstance() throws undeclared checked exception
-
- Closed
-
-
JDK-6195208 (reflect spec) Clarify Class.newInstance() behavior for exceptions
-
- Closed
-
-
JDK-5066799 Class.newInstance spec should highlight checked exception problem
-
- Resolved
-