This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes arithmetic exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arithmetic exception" Watch "arithmetic exception" New topic

arithmetic exception

ronak mehta

Joined: Aug 14, 2001
Posts: 23
1) ArithmeticException will occur after execution of the following code
int i = 10;
float f = 0.0;
double d = i/f;

this is the answer and i can't understand it .i think that float f is 0 so divide by zero give arithmetic exception. may be this answer is becoz it give runtime error not exception am i right?
ronak please help.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9058
integers give an ArithmeticException when division by zero occurs. A float divided by zero will give positive infinity, negative infinity or NaN.

This question does not belong in the JavaRanch forum. The JavaRanch forum is for discussion of the JavaRanch site, not questions about Java. I'm moving this to Java in General (beginner).

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
In theory 0.0f is a rounding of some number with a zillion decimal places but that is not necessarily zero. Therefore the division is legal, just results in a VERY SMALL or VERY LARGE numbers. That is why the answer comes back as POSITIVE_INFINITY.

"JavaRanch, where the deer and the Certified play" - David O'Meara
It is sorta covered in the JavaRanch Style Guide.
subject: arithmetic exception
It's not a secret anymore!