Java - Differences Between Checked and Unchecked Exceptions

By Shahnail Khan Last updated : March 22, 2024

In Java, exceptions help us deal with errors or unusual situations that occur when a program runs. There are two main types of exceptions: checked and unchecked. Knowing how they differ is important for writing good Java programs.

Java - Checked Exception

Checked exceptions, also known as compile-time exceptions, are exceptions that must be either caught or declared in the method signature using the throws keyword.

These exceptions typically represent error conditions that are outside the program's control but are expected to occur and need to be handled gracefully. Examples of checked exceptions include problems like dealing with files (IOException), databases (SQLException), or missing classes (ClassNotFoundException).

Example of Checked Exception in Java

Let's say you're trying to read from a file, but the file isn't there:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    try {
      File file = new File("example.txt");
      Scanner scanner = new Scanner(file);
      while (scanner.hasNextLine()) {
        System.out.println(scanner.nextLine());
      }
      scanner.close();
    } catch (FileNotFoundException e) {
      System.out.println("The file is missing: " + e.getMessage());
    }
  }
}

The output of the above example is:

The file is missing: example.txt (No such file or directory)

Here, FileNotFoundException is a checked exception because you must deal with it or declare that it might happen.

Java - Unchecked Exception

Unchecked exceptions, also known as runtime exceptions, are exceptions that do not need to be caught or declared explicitly.

These exceptions usually represent programming errors or conditions that are beyond our control. Examples of unchecked exceptions in Java include NullPointerException, ArrayIndexOutOfBoundsException, and IllegalArgumentException.

Example of Unchecked Exception in Java

Let's take an example to understand this better.

public class Main {
  public static void main(String[] args) {
    int[] numbers = {
      1,
      2,
      3
    };
    System.out.println(numbers[4]); // ArrayIndexOutOfBoundsException
  }
}

The output of the above example is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 3
	at Main.main(Main.java:8)

In this example, accessing an index outside the bounds of the array results in an ArrayIndexOutOfBoundsException, which is an unchecked exception.

Differences Between Checked and Unchecked Exceptions in Java

Sr. No. Checked Exceptions Unchecked Exceptions
1. Must be handled or declared. Not required to be explicitly handled.
2. Detected by the compiler. Not checked by the compiler.
3. Usually expected errors. Often unexpected issues.
4. Examples: IOException, SQLException. Examples: NullPointerException, ArrayIndexOutOfBoundsException.
5. Helps maintain program stability. Can lead to program crashes if not handled properly.
6. Adds flexibility in error handling. Requires stricter programming practices.
7. Detected at compile time. Occur during runtime.
8. Developers must ensure proper handling. Developers should avoid causing them through careful programming.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.