What is the difference between run-time error and compiler error?

JavaCompiler Errors

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

A run time error will only occur when the code is actually running. These are the most difficult - and lead to program crashes and bugs in your code which can be hard to track down.

An example might be trying to convert a string: "hello" into an integer:

string helloWorld = "hello";
int willThrowRuntimeError = Convert.ToInt32(helloWorld);

The compiler may not see this as a problem but when run an error will be thrown.

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.

An example of a compiler error would be:

int = "this is not an int";

Hope that helps.

Solution 2 - Java

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.

Solution 3 - Java

Compile time errors refers to syntax and semantics. For example, if you do operations that involves different types. Ex: adding a string with an int, or dividing a string by a real. (read the last paragraph thou!!!)

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.

This is a very broad explanation. There are many smart compilers, and, also, is possible to do internal casting among different types that leads to operations that make sense. It it possible to pre-compile code and see some run time errors even if the code is not executed.

Refer to this link too: <https://stackoverflow.com/questions/846103/runtime-vs-compile-time>

Solution 4 - Java

Compile Time error means that the Compiler knows that discountVariable = saleVariable must be end with a semi colon as below discountVariable = saleVariable;so it will throw an error when you compile the code.

Run Time error means that the error will occur at run time, because even though you are casting saleVariable into discountVariable, the cast cannot take because they differ in type.

Solution 5 - Java

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.

Solution 6 - Java

think you've already got the general desc of what's the difference. Specifically in the code you have shown in the OP,

  • In second statement, compiler compares the types on LHS and RHS and finds no implicit cast possible so it gives the error.
  • first statement is seen by compiler as the same, but here programmer explicitly casts the type, which is as good as telling compiler that I know what I'm doing and of course the compiler trusts you and gives you no errors.

Solution 7 - Java

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.

Solution 8 - Java

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 as Int.

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

Solution 9 - Java

If you'd use Google, you'd get this:

> Compile time error is any type of error that prevent a java program compile like a syntax error, a class not found, a bad file name for the defined class, a possible loss of precision when you are mixing different java data types and so on. > > A runtime error means an error which happens, while the program is running. To deal with this kind of errors java define Exceptions. Exceptions are objects represents an abnormal condition in the flow of the program. It can be either checked or unchecked.

http://wiki.answers.com/Q/Difference_between_run_time_error_and_compile_time_error_in_java

Solution 10 - Java

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

class OverloadingTest {
	void sum(int a, long b) {
		System.out.println("a method invoked");
	}

	void sum(long a, int b) {
		System.out.println("b method invoked");
	}

	public static void main(String args[]) {
		OverloadingTest obj = new OverloadingTest();
		obj.sum(200, 200);// now ambiguity
	}
}

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbdullahRView Question on Stackoverflow
Solution 1 - JavaDIXONJWDDView Answer on Stackoverflow
Solution 2 - JavaJames MontagneView Answer on Stackoverflow
Solution 3 - Javauser935375View Answer on Stackoverflow
Solution 4 - JavaCodeBlueView Answer on Stackoverflow
Solution 5 - JavaHadiView Answer on Stackoverflow
Solution 6 - JavaKashyapView Answer on Stackoverflow
Solution 7 - JavabigamilView Answer on Stackoverflow
Solution 8 - JavaPankaj PrakashView Answer on Stackoverflow
Solution 9 - Javauser647772View Answer on Stackoverflow
Solution 10 - JavaNikhil KumarView Answer on Stackoverflow