63

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?

The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
AbdullahR
  • 1,011
  • 3
  • 13
  • 14
  • 67
    Come on guys. The guy is trying to learn, downvotes are unnecessary. – JohnFx Feb 27 '12 at 20:35
  • 8
    Doesn't matter if it has addressed my example or not, what really matters is that I got the answer clearly. That guy explained it in a simple way that can be easy to understand to a naive programmer like me. – AbdullahR Feb 27 '12 at 20:54
  • Did you read the http://stackoverflow.com/faq ? –  Feb 27 '12 at 21:00

10 Answers10

66

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";
starball
  • 20,030
  • 7
  • 43
  • 238
DIXONJWDD
  • 1,276
  • 10
  • 20
  • _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._ This is true relative to compile-time errors but [runtime errors](https://en.wikipedia.org/wiki/Runtime_error_detection) are much easier to debug than a program that produces invalid results silently, which is typically the most difficult class of bugs to identify and resolve. – ggorlen Dec 19 '20 at 19:22
18

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.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
  • 2
    In your example, `discountVariable` is of declared type `DiscountSale` and `saleVariable` is of another type. When you try to assign the value of one to the other with the line `discountVariable = saleVariable`, this causes a compiler error. If you put the cast in place `(DiscountSale)`, this tells the compiler that it's okay. It then breaks when you run it because `saleVariable` is not actually of the `DiscountSale` type. – Erick Robertson Feb 27 '12 at 20:52
  • 2
    +1 because this is the simplest best answer to the question: What is the difference between a runtime error and a compiler error? I would be much happier if the answer actually addressed the specific example offered by the OP, however. – Erick Robertson Feb 27 '12 at 20:54
12

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: Runtime vs Compile time

Community
  • 1
  • 1
3

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.

Hadi
  • 520
  • 9
  • 21
3

Compile Time error means that the Compiler knows that discountVariable = saleVariable must be end with a semi colon as belowdiscountVariable = 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.

Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58
CodeBlue
  • 14,631
  • 33
  • 94
  • 132
2

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.
Kashyap
  • 15,354
  • 13
  • 64
  • 103
1

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

Pankaj Prakash
  • 2,300
  • 30
  • 31
1

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.

bigamil
  • 657
  • 4
  • 12
  • 1
    Not a +1 because "the compiler doesn't know the object type of saleVariable". That's not true. The compiler knows the type - it's just incompatable with DiscountSale. – Erick Robertson Feb 27 '12 at 20:57
  • Your right, I was just thinking of it in context of if he was doing something like e.DataItem – bigamil Feb 28 '12 at 14:27
0

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

Nikhil Kumar
  • 2,618
  • 3
  • 21
  • 24
0

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

Community
  • 1
  • 1