Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 391
AbdullahR Avatar asked Feb 27 '12 20:02

AbdullahR


People also ask

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

A compiler cannot easily detect a runtime error. Thus, we need to identify it during the execution of code. A compile-time error generally refers to the errors that correspond to the semantics or syntax. A runtime error refers to the error that we encounter during the code execution during runtime.

What is the difference between run time and compile-time?

Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.

What is a run time error?

A runtime error is a software or hardware problem that prevents Internet Explorer from working correctly. Runtime errors can be caused when a website uses HTML code that's incompatible with the web browser functionality.

What is the difference between runtime error and logical error?

Definition. A runtime error is an error that occurs while running a computer program while a logical error is an error in a program that causes it to operate incorrectly, but not to terminate abnormally. This is the main difference between runtime error and logical error.


2 Answers

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.

like image 121
DIXONJWDD Avatar answered Sep 21 '22 09:09

DIXONJWDD


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.

like image 25
James Montagne Avatar answered Sep 21 '22 09:09

James Montagne