我有一些应从引发的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?),但没有帮助。
最佳答案
我认为,如果您首先将原因分配给局部变量,FindBugs将能够正确找出原因:
try {
/* ... */
} catch (IllegalAccessException | InvocationTargetException
| NoSuchMethodException | NoSuchFieldException e) {
Throwable cause = e.getCause();
if (cause instanceof CustomException) {
throw (CustomException) cause;
}
throw new IllegalArgumentException();
}