Java division by zero doesnt throw an ArithmeticException - why?

JavaArithmeticexception

Java Problem Overview


Why doesn't this code throw an 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!

Java Solutions


Solution 1 - Java

IEEE 754 defines 1.0 / 0.0 as Infinity and -1.0 / 0.0 as -Infinity and 0.0 / 0.0 as NaN.

By the way, floating point values also have -0.0 and so 1.0/ -0.0 is -Infinity.

Integer arithmetic doesn't have any of these values and throws an Exception instead.

To check for all possible values (e.g. NaN, 0.0, -0.0) which could produce a non finite number you can do the following.

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

Solution 2 - Java

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!");
    }

Solution 3 - Java

That's because you are dealing with floating point numbers. Division by zero returns Infinity, which is similar to NaN (not a number).

If you want to prevent this, you have to test tab[i] before using it. Then you can throw your own exception, if you really need it.

Solution 4 - Java

0.0 is a double literal and this is not considered as absolute zero! No exception because it is considered that the double variable large enough to hold the values representing near infinity!

Solution 5 - Java

Java will not throw an exception if you divide by float zero. 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.

Solution 6 - Java

When divided by zero

  1. If you divide double by 0, JVM will show Infinity.

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

Console: Infinity

  1. If you divide int by 0, then JVM will throw Arithmetic Exception.

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

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

Solution 7 - Java

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.

Solution 8 - Java

This is behaviour of floating point arithmetic is by specification. Excerpt from the specification, § 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKatieView Question on Stackoverflow
Solution 1 - JavaPeter LawreyView Answer on Stackoverflow
Solution 2 - JavaWaqas IlyasView Answer on Stackoverflow
Solution 3 - Javagd1View Answer on Stackoverflow
Solution 4 - JavacodeManView Answer on Stackoverflow
Solution 5 - JavaKhalilView Answer on Stackoverflow
Solution 6 - JavaRaman GuptaView Answer on Stackoverflow
Solution 7 - Javauser3937538View Answer on Stackoverflow
Solution 8 - JavawilxView Answer on Stackoverflow