Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

divide by zero: Not a compile time error

 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there:
Why divide by zero not a compile time error. I
always thought it was. but following code gives
run time exception:
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the compiler doesn't check for divide by zero.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On Java 1.1.8 , I get a compiler error ("Arithmetic exception"). Does this not happen in later compilers?
 
zarina mohammad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its Arthmetic Exception ...not compiler error.
(tried on java 1.4)
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ron Newman:
On Java 1.1.8 , I get a compiler error ("Arithmetic exception"). Does this not happen in later compilers?
If your compiler is throwing exceptions then you have serious problems.
 
Venkatesh Gowri
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
public class divbyzero
{
public static void main(String args[])
{
int x=10;
int y=10/0;
System.out.println(y);
}
}
The java compiler dosent even care about the Values u havce assigned.This is taken care of at Run time .So the JVM throws a Runtime exception.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I thought expressions such as "10/2" are compile-time constants -- the compiler evaluates them directly rather than generating code to compute them.
Why would "10/0" be different?
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's more like the compiler substitutes the constants into the code so that it won't look up the values anymore during execution. It still evaluates the expression at runtime.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic