问题描述
我所依赖的代码中有一些不兼容的更改。
所以我想捕获一个NoSuchMethodException来记录有关该问题的更多信息
There are some incompatible changes in code which I depend on.So I want to catch a NoSuchMethodException to log more information about the problem
当我使用此方法时:
try{
do.something();
}catch(NoSuchMethodException e){
System.out.println("!");
}
我收到此错误 NoSuchMethodException无法到达的catch块。永远不会抛出此异常从try语句主体中
I get this error "Unreachable catch block for NoSuchMethodException. This exception is never thrown from the try statement body"
我也尝试捕获java.lang.RuntimeException并检查它是否是NoSuchMethod,但没有用。
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....
有什么想法吗?
推荐答案
您对NoSuchMethodException感到困惑,该异常仅在调用使用反射不存在的方法时引发,而在直接调用方法时可能引发的NoSuchMethodError此方法在编译时存在,在运行时不存在。当在编译和运行时使用某些外部库的不同版本时,通常会发生这种情况。
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.
底线:catch NoSuchMethodError
这篇关于如何捕获NoSuchMethodException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!