我应该如何在 Java 中抛出除以零异常而不实际除以零?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1657887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How should I throw a divide by zero exception in Java without actually dividing by zero?
提问by Eric
I have an I2C device that wants two inputs: a denominator and a numerator. Both are written to separate addresses, so no actual calculation (numerator/denominator
) is done. The problem with this is that a divide by zero could occur on the I2C device, so a divide by zero error needs to be checked for. Ideally, exactly the same thing would happen if the dividing were done by the java code.
我有一个需要两个输入的 I2C 设备:一个分母和一个分子。两者都写入不同的地址,因此不进行实际计算 ( numerator/denominator
)。这样做的问题是在 I2C 设备上可能发生被零除,因此需要检查被零除错误。理想情况下,如果分割是由 java 代码完成的,则会发生完全相同的事情。
At the moment, I've bodged an unused variable that does the division, but I'm worried it'll get optimized out:
目前,我已经找到了一个未使用的变量来进行除法,但我担心它会得到优化:
public void setKp(int numerator, int divisor)
{
int zeroCheck = numerator / divisor;
//... doesn't use zeroCheck
}
Surely there's a better way!
肯定有更好的方法!
采纳答案by Joren
You should notthrow an ArithmeticException. Since the error is in the supplied arguments, throw an IllegalArgumentException
. As the documentationsays:
你应该不是抛出一个ArithmeticException。由于错误在提供的参数中,因此抛出IllegalArgumentException
. 正如文档所说:
Thrown to indicate that a method has been passed an illegal or inappropriate argument.
抛出以指示方法已传递非法或不适当的参数。
Which is exactly what is going on here.
这正是这里发生的事情。
if (divisor == 0) {
throw new IllegalArgumentException("Argument 'divisor' is 0");
}
回答by uckelman
Do this:
做这个:
if (denominator == 0) throw new ArithmeticException("denominator == 0");
ArithmeticException is the exception which is normally thrown when you divide by 0.
ArithmeticException 是除以 0 时通常抛出的异常。
回答by Bart Kiers
Something like:
就像是:
if(divisor == 0) {
throw new ArithmeticException("Division by zero!");
}
回答by Jo?o Silva
public class ZeroDivisionException extends ArithmeticException {
// ...
}
if (denominator == 0) {
throw new ZeroDivisionException();
}
回答by slangevi
There are two ways you could do this. Either create your own custom exception class to represent a divide by zero error or throw the same type of exception the java runtime would throw in this situation.
有两种方法可以做到这一点。要么创建您自己的自定义异常类来表示除以零错误,要么抛出 Java 运行时在这种情况下会抛出的相同类型的异常。
Define custom exception
定义自定义异常
public class DivideByZeroException() extends ArithmeticException {
}
Then in your code you would check for a divide by zero and throw this exception:
然后在您的代码中,您将检查除以零并抛出此异常:
if (divisor == 0) throw new DivideByZeroException();
Throw ArithmeticException
抛出算术异常
Add to your code the check for a divide by zero and throw an arithmetic exception:
将检查除以零并抛出算术异常添加到您的代码中:
if (divisor == 0) throw new java.lang.ArithmeticException("/ by zero");
Additionally, you could consider throwing an illegal argument exception since a divisor of zero is an incorrect argument to pass to your setKp() method:
此外,您可以考虑抛出非法参数异常,因为除数为零是传递给 setKp() 方法的不正确参数:
if (divisor == 0) throw new java.lang.IllegalArgumentException("divisor == 0");