Community
Participate
Working Groups
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.
*** This bug has been marked as a duplicate of bug 73244 ***
The quick fix now suggests to document the thrown exception to avoid the 'unused' warning. Verified for 3.4M2 using I20070917-1800