JDK-6490045 : Checked exceptions can be thrown without declaring them
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2006-11-03
  • Updated: 2010-04-04
  • Resolved: 2006-11-04
Related Reports
Duplicate :  
Description
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.