This question already has an answer here:
- Runtime vs Compile time 24 answers
In one of my prof slides on ploymorphism, I see this piece of code with a couple of comments:
discountVariable = //will produce
(DiscountSale)saleVariable;//run-time error
discountVariable = saleVariable //will produce
//compiler error
As you can see, it says in the first casting statement that it'll produce run-time error and in the other one it says it'll produce compiler error.
What makes these errors? and how they differ from each other?
Compile time errors are errors of syntax and semantics.
Run time errors are errors of logic primarily. Due to something the programmer has overlooked, the program crashes e.g. division by 0, accessing a variable without initializing it first etc.
Its because the compiler doesn't know the object type of "saleVariable" until that value has actually been set when the program is running.
Your are forcing whatever is in salesVariable into the type DiscountSale this is considered unsafe and cannot be evaluated until runtime.
A runtime error happens during the running of the program. A compiler error happens when you try to compile the code.
If you are unable to compile your code, that is a compiler error.
If you compile and run your code, but then it fails during execution, that is runtime.
Compilation/Compile time/Syntax/Semantic errors: Compilation or compile time errors are error occurred due to typing mistake, if we do not follow the proper syntax and semantics of any programming language then compile time errors are thrown by the compiler. They wont let your program to execute a single line until you remove all the syntax errors or until you debug the compile time errors.
Example: Missing a semicolon in C or mistyping
int
asInt
.Runtime errors: Runtime errors are the errors that are generated when the program is in running state. These types of errors will cause your program to behave unexpectedly or may even kill your program. They are often referred as Exceptions.
Example: Suppose you are reading a file that doesn't exist, will result in a runtime error.
Read more about all programming errors here
Compiler errors are due to inaccuracies in code, where the compiler throws an error to alert you to something which will not compile, and therefore cannot be run.
Ex :- MethodOverloading
Run Time errors are those that are detected when the program execute. For example, division by zero. The compiler can not know if the operation x/a-b will leads to division by zero until the execution
If you'd use Google, you'd get this:
http://wiki.answers.com/Q/Difference_between_run_time_error_and_compile_time_error_in_java