处理Java中的运行时异常 [英] Handling Runtime exception in Java

查看:358
本文介绍了处理Java中的运行时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

 try{//do something
     }
  catch (Exception e) {
        log.error(e, e);
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException(e);
        }
    }

findbugs stataic分析工具会在此引发此警告

the findbugs stataic analysis tool throws this warning on it


instanceof将始终为methodX中的所有非空值返回true,因为所有RuntimeException都是RuntimeException的实例

instanceof will always return true for all nonnull values in methodX, since all RuntimeException are instances of RuntimeException

我不明白的是它的异常被抓住而不是RuntimeException,所以为什么这个警告?

what i dont understand is that its Exception which is being caught and not the RuntimeException, so why this warning ?

推荐答案

您还可以尝试以下代码。这将更好地阅读和维护。

You could also try following code. This will be better to read and maintain.

try{//do something
}
catch (RuntimeException e) {
    throw e;
} 
catch (Exception e) {
    throw new RuntimeException(e);
}

这篇关于处理Java中的运行时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆