What is the 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

Sai Subramanyam
Sai Subramanyam

Passionate, Curious and Enthusiastic.

Updated on: 30-Jul-2019

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements