Bug 187621 - compiler does not compute exception intersection; emits "Unhandled exception type CloneNotSupportedException" error
Summary: compiler does not compute exception intersection; emits "Unhandled exception ...
Status: VERIFIED DUPLICATE of bug 79798
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2.2   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.4 M7   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-05-17 13:59 EDT by David Biesack CLA
Modified: 2008-04-29 07:32 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Biesack CLA 2007-05-17 13:59:15 EDT
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();
    }
}
Comment 1 Kent Johnson CLA 2007-05-18 11:32:51 EDT

*** This bug has been marked as a duplicate of bug 79798 ***
Comment 2 David Audel CLA 2008-04-29 07:32:08 EDT
Verified for 3.4M7 using I20080427-2000