Java Checked vs Unchecked Exceptions

Last Updated : 1 Sep, 2026

Exceptions are events that interrupt the normal flow of program execution. Based on whether the compiler requires them to be handled or declared, exceptions are mainly classified into checked exceptions and unchecked exceptions.

  • Checked exceptions are checked by the compiler at compile time.
  • Unchecked exceptions occur at runtime and are not required to be handled or declared.
  • Checked exceptions generally represent conditions that a program may reasonably need to recover from, such as file or database errors.
Types of Exceptions in Java

Checked Exceptions

Checked exceptions are exceptions that are checked by the compiler at compile time. If a method can throw a checked exception, it must be either handled using a try-catch block or declared using the throws keyword.

  • Derived from the Exception class (excluding RuntimeException and its subclasses).
  • Help ensure that exceptional conditions are anticipated and handled properly.

Commonly Occurred Checked Exceptions

  • IOException: Occurs when an input/output operation fails, such as reading from or writing to a file.
  • SQLException: Occurs when there is an error while interacting with a database.
  • ClassNotFoundException: Thrown when the JVM cannot find a specified class at runtime.
  • FileNotFoundException : Occurs when a program attempts to access a file that does not exist.
  • InterruptedException: Thrown when a thread is interrupted while waiting, sleeping, or performing certain operations.

Checked exceptions represent invalid conditions in areas outside the immediate control of the program like memory, network, file system, etc. Any checked exception is a subclass of Exception.

Java
import java.io.*;

class Geeks 
{
    public static void main(String[] args) 
    {
        // Getting the current root directory
        String root = System.getProperty("user.dir");
        System.out.println("Current root directory: " + root);

        // Adding the file name to the root directory
        String path = root + "\\message.txt";
        System.out.println("File path: " + path);

        // Reading the file from the path in the local directory
        FileReader f = new FileReader(path);

        // Creating an object as one of the ways of taking input
        BufferedReader b = new BufferedReader(f);

        for (int counter = 0; counter < 3; counter++)
            System.out.println(b.readLine());

        f.close();
    }
}

Output: 

Exceptions
Checked Exceptions

To fix the above program, we either need to specify a list of exceptions using throws or we need to use a try-catch block. We have used throws in the below program. Since FileNotFoundException is a subclass of IOException, we can just specify IOException in the throws list and make the above program compiler-error-free.

Java
import java.io.*;
class Geeks {

    public static void main(String[] args) throws IOException {

        // Getting the current root directory
        String root = System.getProperty("user.dir");
        System.out.println("Current root directory: " + root);

        // Adding the file name to the root directory
        String path = root + "\\message.txt";
        System.out.println("File path: " + path);

        // Reading the file from the path in the local directory
        try {
            FileReader f = new FileReader(path);

            // Creating an object as one of the ways of taking input
            BufferedReader b = new BufferedReader(f);

            // Printing the first 3 lines of the file
            for (int counter = 0; counter < 3; counter++)
                System.out.println(b.readLine());

            f.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not found: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("An I/O error occurred: " + e.getMessage());
        }
    }
}

Output: 

OutputExceptionHandling
Checked Exceptions

Note: If you are running this code on Linux/macOS, use the correct path format i.e. /home/user/test/a.txt

Explanation: In the above program we create a Java program which reads a file from the same directory this program may throw exceptions like FileNotFoundException or IOException so we handle it using the try-catch block to handle the exceptions and execute the program without any interruption.

Unchecked Exceptions

Unchecked exceptions are exceptions that are not checked by the compiler at compile time. They occur during program execution and usually result from programming errors or incorrect logic.

  • They are subclasses of RuntimeException.
  • Handling them is optional and not enforced by the compiler.

Commonly Occurred Unchecked Exceptions:

Consider the following Java program. It compiles fine, but it throws an ArithmeticException when run. The compiler allows it to compile because ArithmeticException is an unchecked exception.

Java
class Geeks {
    public static void main(String args[]) {
      
        // Here we are dividing by 0 which will not be caught at compile time as there is no mistake but caught at runtime because it is mathematically incorrect
        int x = 0;
        int y = 10;
        int z = y / x;
    }
}

Output:

Exceptions
Unchecked Exceptions

Explanation: The program compiles successfully because ArithmeticException is an unchecked exception. However, when the program runs, dividing 10 by 0 throws an ArithmeticException.

Note :

  • Unchecked exceptions are runtime exceptions that are not required to be caught or declared in a throws clause.
  • These exception are caused by programming errors, such as attempting to access an index out of bounds in an array or attempt to divide by zero.
  • Unchecked exceptions include all subclasses of the RuntimeException class, as well as the Error class and its subclasses.

Checked Exception vs Unchecked Exceptions

Checked ExceptionUnchecked Exception
Checked by the compiler at compile time.Occurs during program execution.
Must be handled or declared using throws.Does not need to be handled or declared.
Subclass of Exception but not RuntimeException.Subclass of RuntimeException.
Commonly represents conditions such as file or database failures.Commonly results from programming errors.
Examples: IOException, SQLException, ClassNotFoundException.Examples: NullPointerException, ArithmeticException, NumberFormatException.


Comment