What is the difference between run-time error and

2019-01-16 08:43发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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.



回答5:

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.



回答6:

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



回答7:

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.



回答8:

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.


回答9:

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



回答10:

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