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 中创建日期的正确方法是什么? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
