January 6, 2010

Division by zero in Java

Incredibly, the following code will compile without any problems:


int x = 7/0;
System.out.println(x);

int y = 0;
int z = 7/y;
System.out.println(z);


When the inevitable runtime error occurs, it appears as an ArithmeticException; there is no such thing as DivideByZeroException.

No comments:

Post a Comment