Exception in thread "main" java.lang.ArithmeticException: / by zero
I think I've seen it in a few Java books, so I've always had this impression that it's never ok to divide a number by zero. Until I started preparing for the SCJP, that is. I ran the code below.
[java]
import java.io.*;
class TestDivideByZero
{
public static void main(String[] arg) throws IOException
{
System.out.println(1/0.0); //prints "Infinity"
System.out.println(1.0/0); //prints "Infinity"
System.out.println(-1/0.0); //prints "-Infinity"
System.out.println(-1.0/0); //prints "-Infinity"
System.out.println(1/0); //runtime error here
System.out.println(-1/0); //runtime error here
}
}
So what's up? According to Sun:
Despite the fact that overflow, underflow, division by zero, or loss of information may occur, evaluation of a floating-point division operator / never throws a run-time exception.
No comments:
Post a Comment