File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes compiler and run time errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compiler and run time errors" Watch "compiler and run time errors" New topic

compiler and run time errors

viswanadha Nirmala

Joined: Feb 13, 2002
Posts: 2
hi all,
Regarding SCJP-certification, how do we know whether the particular error is a run time error or a compiler error.Like are there any tips to find it.
Thanking you,
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
You just have to write a lot of java programs and be familiar with what the compiler can and will check.
Also read through the hierarchy of classes derived from the Exceptions class
Greg Brouelette
Ranch Hand

Joined: Jan 23, 2002
Posts: 144
Well, a compile time error is something that does not match the API. So this would be a compile time error:
Vector v = new Vector();
Notice the upper case "A" in AddElement. In the Java API all methods start with lower case letters, only Class names start with upper case. Therefore, this would not compile.
However, run time errors are also known as "logic errors". They are errors where you THINK you coded your program correctly ,and it does compile, but the logic doesn't do what you thought it would.
Example. Let's say I want to bet $50 if my poker hand is a straighflush and $10 if it's just a straight. And I want to bet $5 on all other hands. I may have code like this: (assume all the methods actually exists and all the data types are correct)

OK, that LOOKS right. But the evil bit here is that I didn't use curly braces to delimit my if statements. So even though it LOOKS like my else statement is attached to the if(type >= hand.STRAIGHT) line, it's ACTUALLY attached to the most recent if statment. Which in this case is the if (type >= hand.STRAIGHT_FLUSH) line.
In other words, this is what I THOUGHT I wrote:

But this is what I actually wrote:

That would be an example of a run time error because the code is valid, it will compile, but it doesn't do what you thought the code should do.

For a good Prime, call:<br />29819592777931214269172453467810429868925511217482600306406141434158089
I agree. Here's the link:
subject: compiler and run time errors
It's not a secret anymore!