简体   繁体   English

Java 除以零不会抛出 ArithmeticException - 为什么?

[英]Java division by zero doesnt throw an ArithmeticException - why?

Why doesn't this code throw an ArithmeticException ?为什么这段代码不抛出ArithmeticException Take a look:看一看:

public class NewClass {

    public static void main(String[] args) {
        // TODO code application logic here
        double tab[] = {1.2, 3.4, 0.0, 5.6};

        try {
            for (int i = 0; i < tab.length; i++) {
                tab[i] = 1.0 / tab[i];
            }
        } catch (ArithmeticException ae) {
            System.out.println("ArithmeticException occured!");
        }
    }
}

I have no idea!我不知道!

IEEE 754 defines 1.0 / 0.0 as Infinity and -1.0 / 0.0 as -Infinity and 0.0 / 0.0 as NaN. IEEE 7541.0 / 0.0定义为无穷大,将-1.0 / 0.0为 -Infinity,将0.0 / 0.0为 NaN。

By the way, floating point values also have -0.0 and so 1.0/ -0.0 is -Infinity .顺便说一下,浮点值也有-0.0 ,所以1.0/ -0.0-Infinity

Integer arithmetic doesn't have any of these values and throws an Exception instead.整数算术没有这些值中的任何一个,而是抛出异常。

To check for all possible values (eg NaN, 0.0, -0.0) which could produce a non finite number you can do the following.要检查可能产生非有限数的所有可能值(例如 NaN、0.0、-0.0),您可以执行以下操作。

if (Math.abs(tab[i] = 1 / tab[i]) < Double.POSITIVE_INFINITY)
   throw new ArithmeticException("Not finite");

Why can't you just check it yourself and throw an exception if that is what you want.如果这是你想要的,你为什么不能自己检查它并抛出异常。

    try {
        for (int i = 0; i < tab.length; i++) {
            tab[i] = 1.0 / tab[i];

            if (tab[i] == Double.POSITIVE_INFINITY ||
                    tab[i] == Double.NEGATIVE_INFINITY)
                throw new ArithmeticException();
        }
    } catch (ArithmeticException ae) {
        System.out.println("ArithmeticException occured!");
    }

That's because you are dealing with floating point numbers.那是因为您正在处理浮点数。 Division by zero returns Infinity , which is similar to NaN (not a number).除以零返回Infinity ,它类似于NaN (不是数字)。

If you want to prevent this, you have to test tab[i] before using it.如果你想防止这种情况,你必须在使用之前测试tab[i] Then you can throw your own exception, if you really need it.然后你可以抛出你自己的异常,如果你真的需要它。

0.0 is a double literal and this is not considered as absolute zero! 0.0 是一个双重文字,这不被视为绝对零! No exception because it is considered that the double variable large enough to hold the values representing near infinity!也不例外,因为双变量被认为大到足以容纳表示接近无穷大的值!

Java will not throw an exception if you divide by float zero.如果除以浮点零,Java 不会抛出异常。 It will detect a run-time error only if you divide by integer zero not double zero.仅当您除以整数零而不是双零时,它才会检测到运行时错误。

If you divide by 0.0, the result will be INFINITY.如果除以 0.0,结果将是 INFINITY。

When divided by zero除以零时

  1. If you divide double by 0, JVM will show Infinity .如果将 double 除以 0,JVM 将显示Infinity

     public static void main(String [] args){ double a=10.00; System.out.println(a/0); }

    Console: Infinity控制台: Infinity

  2. If you divide int by 0, then JVM will throw Arithmetic Exception .如果将 int 除以 0,则 JVM 将抛出算术异常

     public static void main(String [] args){ int a=10; System.out.println(a/0); }

    Console: Exception in thread "main" java.lang.ArithmeticException: / by zero控制台: Exception in thread "main" java.lang.ArithmeticException: / by zero

There is a trick, Arithmetic exceptions only happen when you are playing around with integers and only during / or % operation.有一个技巧,算术异常仅在您处理整数时发生,并且仅在 / 或 % 操作期间发生。

If there is any floating point number in an arithmetic operation, internally all integers will get converted into floating point.如果算术运算中有任何浮点数,则内部所有整数都会转换为浮点数。 This may help you to remember things easily.这可能会帮助您轻松记住事情。

This is behaviour of floating point arithmetic is by specification.这是浮点运算的行为是由规范。 Excerpt from the specification, § 15.17.2.摘自规范第 15.17.2 节。 Division Operator / : 分部操作员 / :

Division of a nonzero finite value by a zero results in a signed infinity.非零有限值除以零产生有符号无穷大。 The sign is determined by the rule stated above.符号由上述规则确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么用浮点数(或双精度)将数字除以零不会在Java中抛出java.lang.ArithmeticException:/ - Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java 为什么 Java 中的两个 BigDecimal 数相除会抛出 ArithmeticException? - Why does the division of two BigDecimal numbers in Java throws the ArithmeticException? ArithmeticException除以零…如何解决此方法? - ArithmeticException division by zero… how to fix this method? 为什么我得到java.lang.ArithmeticException:/减零? - Why am I getting java.lang.ArithmeticException: / by zero? 为什么我得到 java.lang.ArithmeticException:除以零 - Why am I getting java.lang.ArithmeticException: divide by zero java.lang.ArithmeticException:/ by零 - java.lang.ArithmeticException: / by zero java.lang.ArithmeticException:除法未定义 - java.lang.ArithmeticException: Division is undefined 在Java中抛出ArithmeticException()的所有无效操作的列表? - List of all invalid operations that throw an ArithmeticException() in Java? Java 数独求解器抛出除以零 ArithmeticException - Java Sudoku solver throws divide by zero ArithmeticException java.lang.ArithmeticException:除以零错误 - java.lang.ArithmeticException: divide by zero error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM