This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArithmeticException in doubles

 
javi cervera
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code :
try {
double a = 10/0;
}
catch(ArithmeticException e)
{
System.out.println("catch");
}
print catch
but this code:
 
javi cervera
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this code:
try {
double a = 10.0/0.0;
}
catch(ArithmeticException e)
{
System.out.println("catch");
}
no print catch
can anybody explain me please ?
thanks in advanced
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rahul Phadnis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this to make it absolutely clear
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since "10/0" is an integer division that produces a compile-time constant, why doesn't the compiler complain?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic