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

initializer blocks cannot throw exceptions

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.3.0
    • tools
    • merlin
    • sparc
    • solaris_2.6
    • Verified

      The code below does not compile because the initializer block can throw an exception. This is explicitly allowed in the language spec (second edition), sec. 8.6, page 188:

      An instance initializer of a named class may not throw a checked
      exception unless that exception or one of its superclasses is
      explicitly declared in the throws clause of each constructor of its
      class and the class has a least one explicitly declared constructor.

      The code meets these criteria, but the compiler does not let it pass.


      import java.io.*;
       
      class TestInitExcept {
       static boolean stuff;
       
       {
        if (stuff)
         throw new IOException("Junk");
       }
       
       TestInitExcept() throws IOException {
       }
       
       public static void main(String[] args) {
        try {
         TestInitExcept t = new TestInitExcept();
        } catch (IOException e) {
         e.printStackTrace();
        }
       }
      }

            gafter Neal Gafter (Inactive)
            karnoldsunw Kenneth Arnold (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: