0

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...

Community
  • 1
  • 1
User27854
  • 824
  • 1
  • 16
  • 40
  • 1
    "compilation error only at the beginning of execution" - no - only when compiling. Runtime error - no - not converting to machine code - logic errors while running the program – Randy Feb 07 '14 at 16:01
  • The compiler does much more than just syntax check. For example, it does type checking, and thus finds many logical errors before execution. – Ingo Feb 07 '14 at 16:10

3 Answers3

1

No, the typical Java virtual machine does not convert byte code to machine code. The created byte code is interpreted.

A run-time error happens if you write a program that is from the point of the programming-language correct (and therefore compiles) but it does something that does not work, e.g. tries to divide something by zero or if the program tirs to access the 4th character of a String that does only contain 3 characters.

Robert
  • 39,162
  • 17
  • 99
  • 152
  • "No, the typical Java virtual machine does not convert byte code to machine code. The created byte code is interpreted." What is the differnce bewtween interpreting and converting.. – User27854 Feb 07 '14 at 16:23
  • The compiler creates "bytecode", which is code for a hypothetical, virtual machine. The JVM can simulate this machine (called "interpreting" the bytecode), or it can convert it to native code using a just-in-time compiler (JIT). – David Conrad Feb 07 '14 at 20:19
1

Your understanding is wrong, but not by much. The names of the errors pretty much indicate their nature.

A compile time error occurs when you compile code. Compiling means converting text into bytecode. This means that your java code can not be converted into bytecode. Compile time errors generally check for syntax, although the Java compiler does some very rudimentary logic checks as well. For example, it does not allow you to compile code with unreachable statements.

A runtime error occurs when you run the program. This is when the JVM is actually trying to execute your compiled bytecode and runs into a problem. There is no step in which the bytecode gets converted directly into machine code. Bytecode is interpreted directly by the JVM. As you indicated, these problems can be things like division by zero and other undefined operations, running out of memory, or sometimes trying to cast objects into illegal types. These are just examples. Anything that prevents bytecode from executing is considered a runtime error.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • "Also, there is no step in which the bytecode gets converted to machine code." You meant to correct me by saying that byte code gets interpreted.. right!! – User27854 Feb 07 '14 at 16:28
  • @user2900314. There are technically ways to turn bytecode into machine code, but they are not generally used because they take away the binary portability that makes Java so useful. When you run a standard JVM, the byte code is interpreted. – Mad Physicist Feb 07 '14 at 16:40
  • Lastly I have one more question for you... Runtime error occurs only when we run the program... after compilation, i.e javac we get a class file and that is interpreted to native/machine code by using an interpreter (java ) isn't this stage called running.. – User27854 Feb 07 '14 at 16:48
  • Yes. Except the machine code. The JVM (java) interprets and executes the bytecode commands. It does not convert them into machine code in the sense of a native application. – Mad Physicist Feb 07 '14 at 17:04
1

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.

AlfredoCasado
  • 818
  • 5
  • 7
  • Runtime error occurs only when we run the program... Oh!! not at this stage java I am sorry I dont know the name of this stage... – User27854 Feb 07 '14 at 16:30
  • There are also compilers that will give warnings for constructs like this (i.e., that do a certain amount of static analysis), but this is a good example. – David Conrad Feb 07 '14 at 20:17