How can I get Eclipse (or javac) to warn about excessive throw suggestions

I am working on a project (someone's code) that has a method declared to throw a bunch of checked exceptions that it might not have thrown.

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 sentence throws

. And, of course, I did not receive any errors from the compiler, because it is impossible to throw these exceptions on the basis of static code analysis.

So my question is, why did Eclipse and its creator, or javac

, not warn me about these false declarations throws

? Is there something I can turn on to make this happen?

Worst of all, if I were getting the warnings I should have received, the effect would be cascading because all callers method()

either re-declare the same throws

or contain a bunch of useless try

/s catch

that absolutely confuse the meaning of the program.

+3


source to share


3 answers


You can force it Eclipse

to throw a compile error / warning if the throws clause contains unnecessary exceptions.

Refer to the following screenshot:



enter image description here

+2


source


This can really break users down, unfortunately, and it can be too costly for Eclipse to find them all.

Part of the problem is that



try {
  cantThrowFooException();
} catch (FooException e) {
  // whatever
}

      

will not compile if it is javac

sure that a FooException

cannot be selected. Therefore, removing the method FooException

from the clause throws

may allow the compiler to prove that it is FooException

not being thrown into the try downstream block, and the result will be unsuccessful compiled.

+1


source


Because the method is not final and I am assuming that it is part of a class that is not final. Thus, it can be overridden by another method that throws these exceptions.

If you changed your question so that any method or class of it is final, you would have another interesting question.

+1


source







All Articles