问题描述
当两个列表的长度不相等时,我需要 throw RuntimeException
.我们正在使用 SonarQube
工具进行代码审查.
I need to throw RuntimeException
when length of two lists is not equal. We are using SonarQube
tool for code review purpose.
代码如下:
if (objctArray.length != columnArray.length) {
throw new RuntimeException(String.format("objctArray and columnArray length is not same. objctArray length = %d, columnArray length = %d", objctArray.length, columnArray.length));
}
现在,SonarQube
在 throw new RuntimeException
行提出了 Define 并抛出专用异常而不是使用通用异常.
的问题.我不知道我可以替换哪个异常来解决 SonarQube
问题.
Now, SonarQube
raises issue that Define and throw a dedicated exception instead of using a generic one.
at throw new RuntimeException
line. I don't know which exception I can replace to resolve SonarQube
issue.
推荐答案
如果这两个列表是传递给方法的参数,IllegalArgumentException
将是一个很好的抛出候选者.它是 RuntimeException
的子类,所以你仍然会抛出一种 RuntimeException
.
If those two lists are arguments passed to a method, IllegalArgumentException
would be a good candidate to throw. It's a sub-class of RuntimeException
, so you'll still be throwing a kind of RuntimeException
.
if (objctArray.length != columnArray.length) {
throw new IllegalArgumentException(String.format("objctArray and columnArray length is not same. objctArray length = %d, columnArray length = %d", objctArray.length, columnArray.length));
}
这篇关于如何解决“定义并抛出专用异常而不是使用通用异常".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!