我应该如何在 Java 中抛出被零除异常而不实际除以零?

发布于 2024-08-09 19:51:45 字数 355 浏览 3 评论 0原文

我有一个 I2C 设备需要两个输入:分母和分子。两者都写入不同的地址,因此不会进行实际计算(分子/分母)。这样做的问题是,I2C 设备上可能会发生被零除的情况,因此需要检查被零除错误。理想情况下,如果由 java 代码完成除法,也会发生完全相同的事情。

目前,我已经使用了一个未使用的变量来进行除法,但我担心它会被优化:

public void setKp(int numerator, int divisor)
{
    int zeroCheck = numerator / divisor;
    //... doesn't use zeroCheck
}

当然有更好的方法!

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.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

说不完的你爱 2024-08-16 19:51:45

您不应该抛出 ArithmeticException。由于错误出现在提供的参数中,因此抛出 IllegalArgumentException。作为文档 说:

抛出该异常表示向方法传递了非法或不适当的参数。

这正是这里发生的事情。

if (divisor == 0) {
    throw new IllegalArgumentException("Argument 'divisor' is 0");
}

You should not throw an ArithmeticException. Since the error is in the supplied arguments, throw an IllegalArgumentException. As the documentation says:

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");
}
你怎么这么可爱啊 2024-08-16 19:51:45

这样做:

if (denominator == 0) throw new ArithmeticException("denominator == 0");

ArithmeticException 是除以 0 时通常抛出的异常。

Do this:

if (denominator == 0) throw new ArithmeticException("denominator == 0");

ArithmeticException is the exception which is normally thrown when you divide by 0.

阳光下慵懒的猫 2024-08-16 19:51:45
public class ZeroDivisionException extends ArithmeticException {
    // ...
}

if (denominator == 0) {
    throw new ZeroDivisionException();
}
public class ZeroDivisionException extends ArithmeticException {
    // ...
}

if (denominator == 0) {
    throw new ZeroDivisionException();
}
左耳近心 2024-08-16 19:51:45

有两种方法可以做到这一点。创建您自己的自定义异常类来表示被零除错误,或者抛出与 Java 运行时在这种情况下抛出的相同类型的异常。

定义自定义异常

public class DivideByZeroException() extends ArithmeticException {
}

然后在您的代码中,您将检查是否被零除并抛出此异常:

if (divisor == 0) throw new DivideByZeroException();

Throw ArithmeticException

将检查是否被零除并抛出算术异常添加到您的代码中:

if (divisor == 0) throw new java.lang.ArithmeticException("/ by zero");

此外,您可以考虑抛出非法参数异常,因为除数为零是传递给 setKp() 方法的错误参数:

if (divisor == 0) throw new java.lang.IllegalArgumentException("divisor == 0");

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.

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:

if (divisor == 0) throw new java.lang.IllegalArgumentException("divisor == 0");
桃扇骨 2024-08-16 19:51:45

像这样的东西:

if(divisor == 0) {
  throw new ArithmeticException("Division by zero!");
}

Something like:

if(divisor == 0) {
  throw new ArithmeticException("Division by zero!");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文