How can I catch a NoSuchMethodException?
There are some incompatible changes in code which I depend on. So I want to catch a NoSuchMethodException to log m开发者_开发知识库ore information about the problem
When I'm using this:
try{
do.something();
}catch(NoSuchMethodException e){
System.out.println("!");
}
I get this error "Unreachable catch block for NoSuchMethodException. This exception is never thrown from the try statement body"
I've tried to catch also java.lang.RuntimeException and check if it's NoSuchMethod but it didn't work.
Reflection will cause performance delays and don't want to use it....
Any ideas?
You was confusing with NoSuchMethodException that is thrown only when invoking method that does not exist using reflection and NoSuchMethodError that may be thrown when you call method directly while this method exists at compile time and does not exist at runtime. It usually happens when using different versions of some external library at compile and runtime.
Bottom line: catch NoSuchMethodError
Catch NoSuchMethodError
instead. However this seems like an ugly workaround and I'd recommend just getting the incompatibility resolved.
EDIT: This serves as a general solution on how to declare checked exceptions. As others have noted, you should catch NoSuchMethodError or even IncompatibleClassChangeError because that is what's gonna be thrown at runtime.
Since you want to link against a different version of that library, than compiling against (not advisable in and of itself, but I'm not one to judge), you need to convice the compiler, that everything is in order.
One way to do that in this case, is to create a static helper that declares the exception:
class Util {
public static void unsafeApiCall() throws NoSuchMethodException {
// if (false) prevents a compilation error
if (false) throw new NoSuchMethodException();
};
}
use like
try {
Util.unsafeApiCall();
do.something();
} catch(NoSuchMethodException e2) {
System.out.println("!");
}
That also stands out visually, which is a good thing.
EDIT2: Such a thing may be nessecary, if you're throwing checked exceptions without declaring them, as described here.
NoSuchMethod should mean that you are trying to access a method that doesn't exist, probably at runtime. Its best if you fix this rather than trying to catch an Exception.
Its better to place the method where its meant to be so that you don't have logical errors like wrong output and so on
I would recommend you to go @matt b's answer. Use that NoSuchMethodError.
But, If at all you want to catch NoSuchMethodException, you will have to throw it explicitly using,
throw new NoSuchMethodException();
Unreachable catch block for NoSuchMethodException. This exception is never thrown from the try statement body
As the IDE/Compiler says. In the try body, there is never a NoSuchMethodException thrown. Which means that none of the methods you are calling is declared as:
public void doSomething() throws NoSuchMethodException
nor there is never literally thrown a NoSuchMethodException
in the try body:
throw new NoSuchMethodException();
First, I strongly disagree with all the people telling you to catch Errors. Don't do that. Errors should be left alone to bubble up to whatever application-wide exception-handler your app has. If your application does that then all you have to do to get your evidence of incompatibility is to grep your log file.
Also, don't pass judgment on code using reflection until you have profiled it and figured out exactly how long it takes. Usually guesses about where slowdowns are end up being incorrect. You should never optimize anything until you have taken measurements and demonstrated that it is a bottleneck.
Dig into the do.something
code, decompiling it if you don't have the source, and see what is actually thrown by it. Possibly if the exception gets thrown it might get wrapped in another exception. Make sure that anything thrown by the method does not get eaten (including by your own logging of it).
精彩评论