aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes divide by zero: Not a compile time error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "divide by zero: Not a compile time error" Watch "divide by zero: Not a compile time error" New topic
Author

divide by zero: Not a compile time error

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: May 05, 2000
Posts: 13974
Because the compiler doesn't check for divide by zero.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
On Java 1.1.8 , I get a compiler error ("Arithmetic exception"). Does this not happen in later compilers?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
Its Arthmetic Exception ...not compiler error.
(tried on java 1.4)
Thomas Paul
mister krabs
Ranch Hand

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

Joined: Aug 15, 2002
Posts: 12
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.


Gowri Venkatesh
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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

Joined: Jun 04, 2002
Posts: 133
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.
 
 
subject: divide by zero: Not a compile time error