Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# ArithmeticException in doubles

javi cervera
Greenhorn
Posts: 16
This code :
try {
double a = 10/0;
}
catch(ArithmeticException e)
{
System.out.println("catch");
}
print catch
but this code:

javi cervera
Greenhorn
Posts: 16
this code:
try {
double a = 10.0/0.0;
}
catch(ArithmeticException e)
{
System.out.println("catch");
}
no print catch
can anybody explain me please ?

Ron Newman
Ranch Hand
Posts: 1056
Floating-point division by 0 doesn't cause an ArithmeticException. It produces an Infinity constant (or a NaN, if you divide 0 by 0).

Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
And just to make it clear:
float f = 10/0; //is integer math even though the result is being moved to a float.
For example:
float f = 10/20; //f equals 0.0 because 10/20 = 0 in integer math

Greenhorn
Posts: 7
Try this to make it absolutely clear

Corey McGlone
Ranch Hand
Posts: 3271
wasarta,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.

Ron Newman
Ranch Hand
Posts: 1056
Since "10/0" is an integer division that produces a compile-time constant, why doesn't the compiler complain?