GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes When a compiler error vs when a runtime error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When a compiler error vs when a runtime error" Watch "When a compiler error vs when a runtime error" New topic
Author

When a compiler error vs when a runtime error

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
In going through these mock exams, I find a lot of questions dealing with compile time errors and run time errors. What is a general rule of thumb for when to expect a compile time error and when to expect a run time error
-Dale
------------------
What's this H2SO4 doing in my fridge?? ( thud )


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521

Well, a compile time error usually happens when you are compiling it.
A run time error usually happens when you are running it.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
I have a hard time with determining if it would be a compile time error or a run-time error.
I do know that the compiler tries to find as much as it can. But, many things for example, things that have to do with the live object being used can only be determined at run-time.
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
That's exactly my point. I'd like to find a general rule to figure out if something would generate a run-time error or a compile-time error.
-Dale
------------------
What's this H2SO4 doing in my fridge?? ( thud )
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Depends on the superclass of the Exception that is being thrown. Check the API
for the Exception you are wondering about to see whether it inherits from RuntimeException or not. If it does not, it is a compile-time Exception.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
all errors related to syntax of commands are compile time
there are more but this one if one of them


sona<br />SCJP
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

Compile time errors - caused by errors in syntax or breaking rules that a, for lack of a better term, "well-formed" java program should follow. Things like illegal type conversions, typos, missing parenthesis, redeclaration of variables, etc. that the compiler can determine to be incorrect during compile time. Can usually be traced back to one line in particular.
Runtime errors are usually related to logic errors. The syntax may be correct and the error may be thrown at a particular line of code, but the cause may be due to the logic encompassed by several lines of code. Things like off-by-one loop errors, improperly casting objects, invoking a method on a null object reference would produce runtime errors.
A clarification to what Marilyn said above: All exceptions occur at runtime. The difference is that the compiler will complain about code that does not explicitly catch Checked exceptions.
Junilu


Junilu - [How to Ask Questions] [How to Answer Questions]
 
GeeCON Prague 2014
 
subject: When a compiler error vs when a runtime error