Difference Between Compile-Time Errors and Run-Time Errors in Java



Compile time errors are syntactical errors in the code which hinders it from being compiled.

Example

public class Test{
   public static void main(String args[]){
      System.out.println("Hello")
   }
}

Output

C:\Sample>Javac Test.java
Test.java:3: error: ';' expected
   System.out.println("Hello")

An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.

Example

import java.io.File;
import java.io.FileReader;

public class FilenotFound_Demo {
   public static void main(String args[]) {
      File file = new File("E://file.txt");
      FileReader fr = new FileReader(file);
   }
}

Output

C:\>javac FilenotFound_Demo.java
FilenotFound_Demo.java:8: error: unreported exception
FileNotFoundException; must be caught or declared to be thrown
   FileReader fr = new FileReader(file);
                   ^
1 error
Updated on: 2019-07-30T22:30:20+05:30

430 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements