REC_CATCH_EXCEPTION错误(未引发异常时捕获异常)

时间:2020-02-03 19:42:49

标签: java spotbugs

我看到一种奇怪的行为,在try块中添加了一些代码,现在由于某种原因,我收到了Spotbug错误REC_CATCH_EXCEPTION(当未抛出Exception时捕获了异常)。 / p>

所以基本上,我的代码是这样的:

while (true) {
    try {
        if (something()) {
            doSomething();
        } else if (somethingElse()) {
            doSomethingElse();
        } else {
            do();
        }
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
}

当我运行上面的代码时,没有出现任何Spotbug错误。但是,当我添加新的else if块时,会出现REC_CATCH_EXCEPTION错误。所以我看到错误的新代码是这样的:

while (true) {
    try {
        if (something()) {
            doSomething();
        } else if (somethingElse()) {
            doSomethingElse();
        } else if (someOtherThing()) {
            doSomeOtherThing();
        } else {
            do();
        }
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
}

除了添加doSomeOtherThing()方法外,其他任何事情都没有真正改变。因此,我不确定为什么会看到此错误。另外,请注意,doSomeOtherThing()方法不会在其中引发或捕获任何异常。

0 个答案:

没有答案