gpt4 book ai didi

java - SonarQube 提示 : Either log or rethrow this exception

转载 作者:行者123 更新时间:2023-11-29 04:40:29 27 4
gpt4 key购买 nike

在将代码与 Maven 集成后,我正在运行 SonarQube 5 进行代码质量检查。

Sonar 提示我应该:

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 似乎有点奇怪。

其次,我最近在使用 Sonar 时遇到了同样的问题。似乎您需要将整个异常作为参数传递给记录器,而不是 e.getMessage() 让 Sonar 意识到您正在记录异常。

试试这个:

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

关于java - SonarQube 提示 : Either log or rethrow this exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39411450/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com