Why doesn't Java throw an Exception when dividing by 0.0?

Java's float and double types, like pretty much any other language out there (and pretty much any hardware FP unit), implement the IEEE 754 standard for floating point math, which mandates division by zero to return a special "infinity" value. Throwing an exception would actually violate that standard.

Integer arithmetic (implemented as two's complement representation by Java and most other languages and hardware) is different and has no special infinity or NaN values, thus throwing exceptions is a useful behaviour there.


The result of division by zero is, mathematically speaking, undefined, which can be expressed with a float/double (as NaN - not a number), it isn't, however, wrong in any fundamental sense.

As an integer must hold a specific numerical value, an error must be thrown on division by zero when dealing with them.


When divided by zero ( 0 or 0.00 )

  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

  2. 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

  3. But if we divide int by 0.0, then JVM will show Infinity:

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

    Console: Infinity

This is because JVM will automatically type cast int to double, so we get infinity instead of ArithmeticException.