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

Checked exceptions in anonymous instance initializers not processed correctly.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1
    • tools
    • sparc
    • solaris_2.5

      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

            wmaddoxsunw William Maddox (Inactive)
            jrose John Rose
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: