4

I wondering to know why this snippet of code has executed without Throwing RuntimeException(exactly ArithmethicException):

Code:

public class Proba {
    public static void main(String[] args) {
        Double d = new Double(5.0);
        try {
            d = d / 0;
        } catch (Exception e) {
            System.out.println("Error division by zero!");
        }
        System.out.println("d = " + d);
    }
}

Output:

d = Infinity

I want to know how it is possibly.

My java version is:

C:\Documents and Settings\Admintemp>java -version
java version "1.7.0"
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) Client VM (build 21.0-b17, mixed mode, sharing)
  • Why this behavior is possible at Java?
catch23
  • 17,519
  • 42
  • 144
  • 217
  • Please refer to previous question: http://stackoverflow.com/questions/14137989/java-division-by-zero-doesnt-throw-an-arithmeticexception-why – Rebecca Nelson Oct 08 '13 at 16:19
  • [I'd hope it doesn't throw an error...](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Double.java#Double.0POSITIVE_INFINITY) – Obicere Oct 08 '13 at 19:16

1 Answers1

13

It is possible because Java follows the IEEE standard for floating-point division.

It is true that integer division by 0 will throw an ArithmeticException, but floating-point division by 0 yields a special floating point value for Infinity.

To elaborate, the JLS, Section 15.17.2 says:

[I]f the value of the divisor in an integer division is 0, then an ArithmeticException is thrown.

and

The result of a floating-point division is determined by the rules of IEEE 754 arithmetic:

(snip)

Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule stated above.

That brings up the question, "Why does IEEE state that it should be Infinity instead of some kind of an error?" Here's IEEE's explanation:

Why doesn't division by zero (or overflow, or underflow) stop the program or trigger an error? Why does a standard on numbers include "not-a-number" (NaN)? The 754 model encourages robust programs. It is intended not only for numerical analysts but also for spreadsheet users, database systems, or even coffee pots. The propagation rules for NaNs and infinities allow inconsequential exceptions to vanish. Similarly, gradual underflow maintains error properties over a precision's range.

When exceptional situations need attention, they can be examined immediately via traps or at a convenient time via status flags. Traps can be used to stop a program, but unrecoverable situations are extremely rare. Simply stopping a program is not an option for embedded systems or network agents. More often, traps log diagnostic information or substitute valid results.

Flags offer both predictable control flow and speed. Their use requires the programmer be aware of exceptional conditions, but flag stickiness allows programmers to delay handling exceptional conditions until necessary.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • It is only for float point number. What about integer at this case? – catch23 Oct 08 '13 at 19:15
  • There is no `int` value that represents infinity. While there is a `float` and `double` representation `Infinity`, there is no such representation for `int` (or `byte`, `short`, or `long`). Evaluating an `int` divide-by-zero as any other legal, meaningful `int` value would be wrong. 1 divided by 0 isn't `Integer.MAX_VALUE` or anything else representable as an `int`. Thus Java's only recourse is to throw an exception. – rgettman Oct 08 '13 at 19:22