Bug 181357 - Compiler preference "Unnecessary declaration of thrown checked exeption" reports false positive
Summary: Compiler preference "Unnecessary declaration of thrown checked exeption" repo...
Status: VERIFIED DUPLICATE of bug 73244
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.4 M2   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-04-05 20:44 EDT by Simon Archer CLA
Modified: 2007-09-18 09:23 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 Simon Archer CLA 2007-04-05 20:44:27 EDT
Using 3.3 M6.

The Java compiler preference "Unnecessary declaration of thrown checked exception" reports false positive violations.  This results in me never enabling this potentially useful check.

Steps...

1.  Set the Java compiler preference "Unnecessary declaration of thrown
    checked exeption" to "Warning".

2.  Create the following two classes:

public class Base {
  public InputStream getInputStream() throws IOException {
    return null;
  }
}

public class Sub extends Base {
  public InputStream getInputStream() throws IOException {
    return new FileInputStream("hello.txt");  //$NON-NLS-1$
  }
}


3.  Notice that the getInputStream() method in the Base class has the following
    warning reported against it:  "The declared exception IOException is not
    actually thrown by the method getInputStream() from type Base".

4.  The warning suggests removing the "throws IOException" clause.  Do that.

5.  Notice that now the getInputStream() method in the Sub class has the
    following error reported against it:  "Exception IOException is not
    compatible with throws clause in Base.getInputStream()".


While I appreciate what this compiler preference is trying to do, I only wish to turn on a compiler preference that:

- I can resolve such that I don't get the orginal warning or error.

- Does not result in a compilation error.


In this particular case the preference is not considering the fact that the getInputStream() method is declared to throws an IOException so that subclass implementations have the option of throwing an IOException if necessary, even though the Base implementation does not need it.

Please consider enhancing this preference to do some additional analysis to ensure that cases such as this are ignored.  Unfortunately in its current form I simply cannot enable it, which is a pity because there are other legitimate cases that it works well for.
Comment 1 Maxime Daniel CLA 2007-04-06 01:47:10 EDT

*** This bug has been marked as a duplicate of bug 73244 ***
Comment 2 Jerome Lanneluc CLA 2007-09-18 09:23:40 EDT
The quick fix now suggests to document the thrown exception to avoid the 'unused' warning.
Verified for 3.4M2 using I20070917-1800