File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# ArithmeticException in doubles

javi cervera
Greenhorn

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

Joined: Jan 17, 2002
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

Joined: Jun 06, 2002
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).

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
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

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Greenhorn

Joined: Aug 13, 2002
Posts: 7
Try this to make it absolutely clear
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
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.

SCJP Tipline, etc.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Since "10/0" is an integer division that produces a compile-time constant, why doesn't the compiler complain?

I agree. Here's the link: http://aspose.com/file-tools

subject: ArithmeticException in doubles