Search code examples
javaeclipseexceptionjavacthrows

How can I make Eclipse (or javac) warn about over-inclusive throws clauses


I am working on a project (someone else's code) in which a method was declared to throw a bunch of checked exceptions it could not possibly throw.

Basically, the method looked like this:

// Assume E1 extends Exception
// Assume E2 extends Exception
// Assume E3 extends Exception
public void method(Object param) throws E1, E2, E3 {
    // Call a bunch of methods, none of which are declared to
    // throw checked exceptions.
}

Of course, I deleted the throws clause. And of course, I received no compiler errors from doing so because throwing those exceptions is impossible based on a static analysis of the code.

So my question is why Eclipse and its builder, or javac, wasn't warning me about these spurious throws declarations? Is there something I can turn on to make this happen?

The worst is that if I was getting the warnings I ought to be getting, there would be a cascading effect because all callers of method() either re-declare the same throws or contain a bunch of useless try/catch blocks which absolutely confuse the meaning of the program.


Solution

  • You can force the Eclipse to give compile time error/warning if the throws clause contains unnecessary exceptions in it.

    Please refer to the following screenshot:

    enter image description here