How to resolve #39;Define and throw a dedicated exception instead of using a generic one.#39;(如何解决“定义并抛出专用异常而不是使用通用异常.)
问题描述
当两个列表的长度不相等时,我需要 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));
}
这篇关于如何解决“定义并抛出专用异常而不是使用通用异常".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何解决“定义并抛出专用异常而不是使用通用异常".
基础教程推荐
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01