SonarQube complains: Either log or rethrow this exception(SonarQube 抱怨:记录或重新抛出此异常)
问题描述
在将代码与 Maven 集成后,我正在运行 SonarQube 5 进行代码质量检查.
I am running SonarQube 5 for code quality check after integrating the code with Maven.
Sonar 抱怨我应该:
Sonar is complaining that I should:
记录或重新抛出此异常.
Either log or rethrow this exception.
在以下代码中:
public static Date convertStringtoDate(String stringDate) {
stringDate = StringUtils.trimToNull(stringDate);
SimpleDateFormat dfm = new SimpleDateFormat("dd-MMM-yyyy");
Date date = null;
if (stringDate != null) {
try {
date = dfm.parse(stringDate);
} catch (Exception e) {
logger.info("Cannot convert String to Date: ",convertStringtoDate(e.getMessage()));
}
}
return date;
}
我在这里错过了什么?
推荐答案
首先,这种行为正确吗?您也尝试在异常消息上调用 convertStringtoDate
似乎有点奇怪.
First of all, is this behaviour correct? Seems a bit weird that you are trying to call convertStringtoDate
on the exception message as well.
其次,我最近在使用 Sonar 时遇到了同样的问题.似乎您需要将整个异常作为参数传递给记录器,而不是 e.getMessage()
让 Sonar 意识到您正在记录异常.
Secondly, I had the same problem with Sonar recently. Seems like you need to pass the whole exception as a parameter to the logger, instead of e.getMessage()
for Sonar to realize you are logging the exception.
试试这个:
public static Date convertStringtoDate(String stringDate){
stringDate = StringUtils.trimToNull(stringDate);
SimpleDateFormat dfm = new SimpleDateFormat("dd-MMM-yyyy");
Date date = null;
if(stringDate!=null){
try {
date = dfm.parse(stringDate);
} catch (Exception e) {
logger.info("Cannot convert String to Date: ", e);
}
}
return date;
}
这篇关于SonarQube 抱怨:记录或重新抛出此异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SonarQube 抱怨:记录或重新抛出此异常
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01