我有一些应该从抛出的InvocationTargetException中检索的自定义异常,我是按照以下方式执行的:
try {
...
}
catch (IllegalAccessException | InvocationTargetException
| NoSuchMethodException | NoSuchFieldException e) {
if (e.getCause() instanceof CustomException) {
throw (CustomException) e.getCause();
}
throw new IllegalArgumentException();
}
但是,findbugs抱怨我:
来自Throwable
的未经检查/未经确认的投射我发现了一个silimar问题(how can resolve dodgy:unchecked/unconfirmed cast in sonar?),但它没有帮助。
答案 0 :(得分:2)
我认为如果您首先将原因分配给局部变量,FindBugs将能够正确解决问题:
try {
/* ... */
} catch (IllegalAccessException | InvocationTargetException
| NoSuchMethodException | NoSuchFieldException e) {
Throwable cause = e.getCause();
if (cause instanceof CustomException) {
throw (CustomException) cause;
}
throw new IllegalArgumentException();
}