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

Checked exceptions can be thrown without declaring them

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 6
    • specification
    • None
    • generic
    • generic

      JLS3 says:

      ===
      11.2.3 Exception Checking

      It is a compile-time error if a method or constructor body can throw some
      exception type E when both of the following hold:

      * E is a checked exception type

      * E is not a subtype of some type declared in the throws clause of the method or
      constructor.
      ===

      Yet the following program with a main method that throws an undeclared checked exception can be compiled and run using JDK 6:

      ===
      public class Test {

          public static void main(String[] args) {
              throwCheckedException(new java.io.IOException());
          }

          private static void throwCheckedException(Throwable t) {
              throwInternal(t, RuntimeException.class);
          }

          private static <T extends Throwable> void throwInternal(Throwable t,
                  Class<T> c) throws T {
              throw (T) t;
          }

      }
      ===

      as evidenced by

      ===
      % javac Test.java
      Note: Test.java uses unchecked or unsafe operations.
      Note: Recompile with -Xlint:unchecked for details.
      % java Test
      Exception in thread "main" java.io.IOException
              at Test.main(Test.java:5)
      ===

      The JLS states that this case should be a compile-time error, not a warning. Hence this is either a javac bug or a spec bug.

            abuckley Alex Buckley
            andreas Andreas Sterbenz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: