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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With