声纳抱怨伐木和重新抛出异常

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

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

我在这里错过了什么?我不是已经记录了异常吗?

 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;
    }

答案 1

你应该这样做:

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);
}

声纳不应该再打扰你了


答案 2

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

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

推荐