Java 声纳抱怨记录和重新抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28122271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:38:38  来源:igfitidea点击:

Sonar complaining about logging and rethrowing the exception

javamavenexception-handlingsonarqubesonarqube-5.0

提问by Nital

I have the following piece of code in my program and I am running SonarQube 5 for code quality check on it after integrating it with Maven.

我的程序中有以下代码段,并且在将其与 Maven 集成后,我正在运行 SonarQube 5 以对其进行代码质量检查。

However, Sonar is complaining that I should Either log or rethrow this exception.

但是,声纳抱怨我应该记录或重新抛出此异常

What am I missing here? Am I not already logging the exception?

我在这里缺少什么?我还没有记录异常吗?

 private boolean authenticate(User user) {
        boolean validUser = false;
        int validUserCount = 0;
        try {
            DataSource dataSource = (DataSource) getServletContext().getAttribute("dataSource");
            validUserCount = new MasterDao(dataSource).getValidUserCount(user);
        } catch (SQLException sqle) {
            LOG.error("Exception while validating user credentials for user with username: " + user.getUsername() + " and pwd:" + user.getPwd());
            LOG.error(sqle.getMessage());
        }
        if (validUserCount == 1) {
            validUser = true;
        }
        return validUser;
    }

采纳答案by abarre

You should do it this way :

你应该这样做:

try {
    DataSource dataSource = (DataSource) getServletContext().getAttribute("dataSource");
    validUserCount = new MasterDao(dataSource).getValidUserCount(user);
} catch (SQLException sqle) {
    LOG.error("Exception while validating user credentials for user with username: " +
            user.getUsername() + " and pwd:" + user.getPwd(), sqle);
}

Sonar shouldn't bother you anymore

声纳不应该再打扰你了

回答by rbitshift

If you believe that SQLException can be safely ignored, then you can add it to the list of exceptions for squid:S1166 rule.

如果您认为可以安全地忽略 SQLException,则可以将其添加到 squid:S1166 规则的例外列表中。

  1. Go to Rule-> Search squid:S1166.
  2. Edit exceptions in Quality Profile.
  3. Add SQLException to the list.
  1. 转到规则-> 搜索鱿鱼:S1166。
  2. 在质量配置文件中编辑例外。
  3. 将 SQLException 添加到列表中。

回答by jim

I stumbled across the same issue. I'm not 100% sure if I'm completely right at this point, but basically you should rethrow or log the complete Exception. Whereas e.getMessage()just gives you the detailed message but not the snapshot of the execution stack.

我偶然发现了同样的问题。我不是 100% 确定我在这一点上是否完全正确,但基本上你应该重新抛出或记录完整的异常。而e.getMessage()只为您提供详细消息,而不是执行堆栈的快照。

From the Oracle docs (Throwable):

来自Oracle 文档(Throwable)

A throwable contains a snapshot of the execution stack of its thread at the time it was created. It can also contain a message string that gives more information about the error. Over time, a throwable can suppress other throwables from being propagated. Finally, the throwable can also contain a cause: another throwable that caused this throwable to be constructed. The recording of this causal information is referred to as the chained exception facility, as the cause can, itself, have a cause, and so on, leading to a "chain" of exceptions, each caused by another.

throwable 包含创建时其线程的执行堆栈的快照。它还可以包含一个消息字符串,提供有关错误的更多信息。随着时间的推移,一个 throwable 可以抑制其他 throwable 的传播。最后,throwable 还可以包含一个原因:另一个导致该 throwable 被构造的 throwable。这种因果信息的记录被称为链式异常设施,因为原因本身可以有一个原因,等等,导致异常“链”,每个异常都是由另一个引起的。

This means the solution provided by abarre works, because the whole exception object (sqle) is being passed to the logger.

这意味着 abarre 提供的解决方案有效,因为整个异常对象 (sqle) 都被传递给记录器。

Hope it helps. Cheers.

希望能帮助到你。干杯。

回答by Daniele

What sonar is asking you to do, is to persist the entire exception object. You can use something like:

声纳要求你做的是持久化整个异常对象。您可以使用以下内容:

    try {
        ...         
    } catch (Exception e) {
        logger.error("Error", e);
    }