引用规则的描述(SonarQube 4.5.5):
// Noncompliant - exception is lost (only message is preserved)
try { /* ... */ }
catch (Exception e) { LOGGER.info(e.getMessage()); }
通过向记录器提供异常类,将堆栈跟踪写入日志。
我们代码库中的问题是:通过遵循告诉,不问原则,我们使用检查异常作为我们认为的正常执行路径的一部分,我们不希望它们导致不合理的大日志消息.
几个例子:服务器响应错误代码,数据库语句执行失败乐观锁定(并发用户)......
我的建议:将此案一分为二。
// Noncompliant - exception is lost (only message is preserved)
try { /* ... */ }
catch (Exception e) { LOGGER.info(e.getMessage()); }
和
// Compliant - exception is lost (only message is preserved) but there is business logic handling the situation
try {
/* ... */
} catch (Exception e) {
LOGGER.info(e.getMessage());
*/ exception handling */
}
规则squid:S00108(代码块不能为空)不会发现问题,因为有一个日志记录语句。
这不合理吗?我错过了什么重要的事情吗?
注意:我重写了问题以澄清我的用例