Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4233093

(reflect spec) Class.newInstance() illegally throws undeclared Exceptions

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.7, 1.4.0, 6
    • core-libs
    • 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)
      ======================================================================

            iris Iris Clark
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: