What is the difference between run-time error and

2019-01-16 08:19发布

This question already has an answer here:

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?

10条回答
成全新的幸福
2楼-- · 2019-01-16 08:42

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.

查看更多
对你真心纯属浪费
3楼-- · 2019-01-16 08:42

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.

查看更多
Juvenile、少年°
4楼-- · 2019-01-16 08:44

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.

查看更多
闹够了就滚
5楼-- · 2019-01-16 08:46

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

查看更多
祖国的老花朵
6楼-- · 2019-01-16 08:52

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

查看更多
手持菜刀,她持情操
7楼-- · 2019-01-16 08:57

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

查看更多
登录 后发表回答