javacompiler-errorsjvm

Need clarification about compilation error and run-time error


I was reading about Run-time error and compilation error from

Runtime vs Compile time and How exactly does java compilation take place?

and what I could infer is, during compilation the compiler does not check for the logic but only the syntax and typo errors and where at run-time the logic is checked and how these are implemented etc.. like division by zero, memory not sufficient.

So if my understanding is right then one get compilation error only during compilation stage and run-time error only while executing the program..

For example let me consider a simple program

public class Try {

public static void main(String[] args) {
    System.out.println("My first program");
    }
}

Now when i compile (i.e javac ) at this stage if any errors are produced then those errors are called compilation errors during which syntax and typo errors are checked.

and the errors produced during the process of converting the byte code to native/machine code (e.e java ) is called run-time error during which program logic is checked.

So one can get compilation error only at the beginning of execution and run-time error only at the second stage(i.e converting byte code to machine code).

Please correct me if my understanding is wrong...


Solution

  • A simple example:

    String myString = null;
    myString.substring(..)
    

    The above code is correct in terms of syntax and types, the compiler shows no error. But when you execute the program with this code (run the program) there is an obvious RUN-time error. The compiler isn't smart enough to detect those bugs, if you want to catch those errors earlier in the development cycle (better before your client execute the program :P) there are tools like static analysis tools or, much better, automatic testing.