Bug 148736 - [compiler] Warn on unused exception in interface
Summary: [compiler] Warn on unused exception in interface
Status: VERIFIED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P5 enhancement (vote)
Target Milestone: 3.7 M7   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-06-26 19:42 EDT by Jason Bennett CLA
Modified: 2011-04-26 09:09 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jason Bennett CLA 2006-06-26 19:42:00 EDT
In Java, a method that goes toward implementing an interface does not have to throw all exceptions defined by that interface. This sometimes makes it hard to keep interface definitions in sync with the methods that implement them (especially when exceptions are eliminated from the implementing code).

It would be nice if Eclipse had a warning for interfaces that indicated for a given method on the interface that "no implementing method throws this exception." 

This way, useless exceptions on interfaces could be eliminated, since any caller to the interface must catch all defined exceptions, whether they are actually thrown or not.
Comment 1 Philipe Mulet CLA 2006-06-27 04:25:26 EDT
This is a nice suggestion, checking that implementations match definitions.

Diagnostic to be reported during method verification.
Comment 2 Philipe Mulet CLA 2006-06-27 04:27:48 EDT
It should say something like:
"Method foo() of type X should declare throwing the exception type Bar, as defined by abstract method foo() of type Y"

Rather than unused, I would suggest MissingAbstractMethodThrownException
Comment 3 Stephan Herrmann CLA 2007-05-20 11:00:03 EDT
I think the original suggestion was not to report anything in 
implementing classes (as Philippe discusses),
but to flag the interface method declaring an 'unnecessary' exception.
But that analysis would require to know "all implementing classes",
which is not possible under Java's open world assumption, right?
Comment 4 Maxime Daniel CLA 2007-05-21 01:18:21 EDT
Right.
Comment 5 Kent Johnson CLA 2007-06-28 15:20:03 EDT
Stephan is right - this is impossible to do.

I suggest we close it as INVALID
Comment 6 Jason Bennett CLA 2007-06-29 01:43:33 EDT
Appreciate everyone's consideration.
Comment 7 Maxime Daniel CLA 2007-09-04 09:00:55 EDT
I believe that we could still do something to improve the situation, if everybody feels like it.

Considering the following code extract:

import java.io.IOException;

interface I {
  void foo() throws IOException; 
}
public class X implements I {
  public void foo() throws IOException { // a
  }
}
class Y extends X {
  public void foo() throws IOException { // b
  }
}

Using the current IDE option 'Unnecessary declaration of thrown checked exceptions' valued to true with 'Check overriding and implementing methods' unchecked, we get no warning. Checking 'Check overriding and implementing methods' brings us two warnings (lines a and b). The former misses the warning on line a, the latter adds the somewhat extraneous warning on line b. A more balanced proposal could be to fully consider that only non-abstract methods have a chance to throw an exception, and to complain on line a even when 'Check overriding and implementing methods' is unchecked, recognizing that X#foo is the highest method in the hierarchy that would be capable of throwing the exception. The behavior on line b would then be tuned according to 'Check overriding and implementing methods'.

(Note that this proposal may have consequences in terms of implementation practicality or performance, which may lead me to take it back. I also know that this introduces a change in the way we handle this warning.)

What do others think about this proposal?
Comment 8 Maxime Daniel CLA 2007-09-04 09:06:05 EDT
More tests show that the following code yields no warning, which should be addressed as well to round up the proposal (I believe this is more or less the point of Philippe's comment #2):

import java.io.IOException;

interface I {
  void foo() throws IOException; 
}
public class X implements I {
  public void foo() {
  }
}

Unless we decide to do both, we do not address even a real part of the original bug, and the proposal of comment #7 should then become a separate bug - or not if we decide it's not worth further investigations.
Comment 9 Maxime Daniel CLA 2007-09-04 09:50:51 EDT
Discussed comment #7 with Philippe. While the proposal could be considered as somewhat continuous with the cases that have no abstract method at all, the arbitrary choice of the topmost concrete method and the disruption that it would bring to the existing behavior were strong enough arguments to decide we would not want to do that.
Also clarified that comment #2 stands as a potential contribution to this bug on its own merits (does not need the comment #8 proposal to bring value).
Keeping the bug open with low priority to see if comment #2 gathers enough support to trigger an investment. Will close as WONTFIX else.
Comment 10 Stephan Herrmann CLA 2009-06-26 15:44:48 EDT
(In reply to comment #9)
> Also clarified that comment #2 stands as a potential contribution to this bug
> on its own merits (does not need the comment #8 proposal to bring value).
> Keeping the bug open with low priority to see if comment #2 gathers enough
> support to trigger an investment.

In contrast to the original proposal, comment #2 is of course doable,
but I wouldn't consider this desirable. Not declaring an exception
that an abstract declaration declares is simply a form of conform
redefinition, which actively states that this method doesn't make
use of the right to throw a given exception. Thus, it is at the same
level as covariant return types. Signalling either as a warning
would more probably disturb people, rather than being appreciated.

> Will close as WONTFIX else.
 
I'd suggest to do so.

Comment 11 Olivier Thomann CLA 2011-04-18 10:26:22 EDT
(In reply to comment #10)
> In contrast to the original proposal, comment #2 is of course doable,
> but I wouldn't consider this desirable. Not declaring an exception
> that an abstract declaration declares is simply a form of conform
> redefinition, which actively states that this method doesn't make
> use of the right to throw a given exception. Thus, it is at the same
> level as covariant return types. Signalling either as a warning
Fully agree with this.

> > Will close as WONTFIX else.
> I'd suggest to do so.
yes.
Closing as WONTFIX.
Comment 12 Olivier Thomann CLA 2011-04-18 10:26:50 EDT
Reopening to close as WONTFIX
Comment 13 Olivier Thomann CLA 2011-04-18 10:27:07 EDT
Closing.
Comment 14 Stephan Herrmann CLA 2011-04-26 09:09:52 EDT
Verified for 3.7M7