findbugs来自Throwable的未经检查/未经证实的投射

时间:2014-09-19 03:37:16

标签: java exception exception-handling findbugs

我有一些应该从抛出的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?),但它没有帮助。

1 个答案:

答案 0 :(得分:2)

我认为如果您首先将原因分配给局部变量,FindBugs将能够正确解决问题:

try {
    /* ... */
} catch (IllegalAccessException | InvocationTargetException 
        | NoSuchMethodException | NoSuchFieldException e) {

    Throwable cause = e.getCause();

    if (cause instanceof CustomException) {
        throw (CustomException) cause;
    }

    throw new IllegalArgumentException();
}