更新时间:2021-09-09 03:18:07
以下哪个是引发异常的正确方法?
Which of the following one is the correct way to throw exception?
我不会使代码复杂化,而是会使用已经抛出的异常。
I wouldn't complicate the code, instead I would use the exception it already throws.
void printDivide(int num, int den) throws ArithmeticException {
System.out.println(num / den);
}
使用不同的异常不仅更加复杂,而且令人困惑。
Using a different exception is not just more complicated, it's confusing.
让我们设置一个不同的示例,然后IllagelArgumentException是非法参数的好选择,例如创建数组
Lets set it is a different example, then IllagelArgumentException is a good choice for an illegal argument, like creating an array
void createArray(int size) {
if (size < 0)
throw IllegalArgumentException("Size must be non-negative " + size);
this.array = new int[size];
}