【发布时间】:2020-02-26 12:00:04
【问题描述】:
我在 Eclipse 中创建了一个下面的 java 服务,并使用 PMD 插件查看了代码,但出现以下错误
** 错误 **
DataflowAnomalyAnalysis: Found 'DD'-anomaly for variable 'result' (lines '80'-'83').
SignatureDeclareThrowsException: A method/constructor should not explicitly throw java.lang.Exception
java 服务
@GetMapping("OCR/GetBarcodeRead")
@ApiOperation("Get result from Barcode Zxing library")
public String getBarcodeRead() throws Exception {
String result = new String();
try {
result = service.zxing();
} catch (Exception e) {
System.out.println(e);
result = "";
}
return result;
}
有人可以帮我解决这个问题吗?提前致谢
【问题讨论】:
-
1) 去掉
= new String(),没必要。此外,您永远不需要调用那个特定的构造函数。 --- 2) 删除throws Exception,因为您的代码实际上并没有抛出其中之一。 -
您可以在 PMD 的参考资料中找到有关它的解释。例如pmd.github.io/latest/… 和pmd.github.io/latest/…。如果您寻求解决此警告的建议,我推荐 sonarlint 插件 sonarlint.org,它为这些情况提供合规的解决方案。
标签: java eclipse spring-boot exception pmd