【发布时间】:2015-10-09 07:16:43
【问题描述】:
引用规则描述(SonarQube 4.5.5):
// Noncompliant - exception is lost (only message is preserved)
try { /* ... */ }
catch (Exception e) { LOGGER.info(e.getMessage()); }
通过向记录器提供异常类,将堆栈跟踪写入日志。
我们代码库中的问题是: 通过遵循Tell, don't ask 原则,我们将检查的异常用作我们认为的正常执行路径的一部分,并且我们不希望它们导致不合理的大日志消息。
几个例子:服务器响应错误代码,数据库语句执行因乐观锁定失败(并发用户)...
我的建议:将此案一分为二。
// 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(代码块不能为空)不会发现问题,因为有一个日志语句。
这不合理吗?我错过了什么重要的事情吗?
注意:我已经重写了问题以澄清我的用例
【问题讨论】:
标签: java sonarqube sonarqube-4.5