Community
Participate
Working Groups
Build ID: M20070212-1330 Steps To Reproduce: 1. create a new basic Java project named "JavaPuzzlers" with a src source folder 2. create Arcane3.java (in the default package) 3. insert the code (see the "More information" The compiler flags line 19: Unhandled exception type CloneNotSupportedException JavaPuzzlers/src Arcane3.java line 19 However, javac (JDK 1.5) compiles this without error. The method f() in Type3 should not have any exceptions (i.e. its signature should be "public void f()") since when an interface (Type3) contains a method from two other interfaces (Type1 and Type2), the exception clauses of the method is computed by intersecting the exceptions. The intersection of "throws CloneNotSupportedException" and "throws InterruptedException" is empty, so Type3.f() does not throw exceptions, so the call t3.f(); does not need a try/catch. From the JLS, the relevant section is http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.6 "A method that overrides or hides another method (ยง8.4.8), including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method." By this rule, Type3.f() cannot throw CloneNotSupportedException because that exception is not part of Type2.f(), and it cannot throw InterruptedException because that exception is not part of Type1.f() For further explanation, see Josh Bloch and Neal Gafter's book, Java Puzzlers. This is Puzzle 38 from the book. (Source is also at http://javapuzzlers.com/java-puzzlers.zip) (I marked this as all platforms, all OS because, although that was the platform where I found this, I doubt it is PC/Windows specific. I see the same compiler error with Eclipse 3.2.1 on Linux.) Source for Arcane3.java: interface Type1 { void f() throws CloneNotSupportedException; } interface Type2 { void f() throws InterruptedException; } interface Type3 extends Type1, Type2 { } public class Arcane3 implements Type3 { public void f() { System.out.println("Hello world"); } public static void main(String[] args) { Type3 t3 = new Arcane3(); t3.f(); } }
*** This bug has been marked as a duplicate of bug 79798 ***
Verified for 3.4M7 using I20080427-2000