Hiprup

What is the difference between checked and unchecked exceptions in Java?

In Java, checked exceptions are verified by the compiler at compile time: any method that can throw one must either handle it in a try-catch block or declare it with the throws keyword, otherwise the code will not compile. Unchecked exceptions — subclasses of RuntimeException — are not verified by the compiler and appear only at runtime, typically because of programming mistakes.

In the exception hierarchy, every class under Exception is checked except RuntimeException and its children; Error types (like OutOfMemoryError) are also unchecked.

  • Checked examplesIOException, SQLException, FileNotFoundException, InterruptedException; these model recoverable external failures such as a missing file or a lost database connection.

  • Unchecked examplesNullPointerException, ArrayIndexOutOfBoundsException, IllegalArgumentException, NumberFormatException; these usually signal bugs that should be fixed, not caught.

  • Compiler behaviour — ignoring a checked exception is a compile-time error; ignoring an unchecked one compiles fine but can crash the program — or fail a test — at runtime.

  • Selenium practice — WebDriver exceptions like NoSuchElementException and TimeoutException extend WebDriverException, which is a RuntimeException, so test code compiles without handling them; utilities like Thread.sleep() or FileUtils.copyFile() for screenshots throw checked exceptions you must handle or declare.

Key terms: checked exception, unchecked exception, RuntimeException, throws, try-catch, compile time, Error, exception hierarchy

import java.io.FileReader;
import java.io.IOException;

public class ExceptionDemo {

    // Checked: compiler forces us to declare (or handle) IOException
    static void readConfig() throws IOException {
        FileReader reader = new FileReader("config.properties");
        reader.close();
    }

    public static void main(String[] args) {
        // 1. Checked exception: must be handled, or the code will not compile
        try {
            readConfig();
        } catch (IOException e) {
            System.out.println("Checked handled: " + e.getClass().getSimpleName());
        }
        // Output: Checked handled: FileNotFoundException

        // 2. Unchecked exception: compiles fine with no try-catch or throws
        try {
            String url = null;
            System.out.println(url.length()); // bug discovered only at runtime
        } catch (NullPointerException e) {
            System.out.println("Unchecked handled: " + e.getClass().getSimpleName());
        }
        // Output: Unchecked handled: NullPointerException
    }
}

readConfig() opens a file, so the compiler forces it to declare throws IOException, and main() must wrap the call in try-catch — deleting either would cause a compile-time error, which is the defining trait of a checked exception. The NullPointerException block compiles without any handling because it is unchecked; the bug (calling length() on a null reference) is discovered only when the line executes.

The program prints FileNotFoundException (an IOException subclass) because config.properties does not exist, showing how a checked exception represents an external, recoverable failure rather than a coding bug.

Anchor your answer in the hierarchy (Throwable splits into Error and Exception, with RuntimeException the unchecked branch under Exception) — interviewers rate candidates higher when they explain the rule, not just list examples. Expect the SDET follow-up "are Selenium exceptions checked or unchecked?" — they are unchecked because WebDriverException extends RuntimeException.

Avoid the classic mistake of saying unchecked exceptions "cannot be caught"; they can be caught like any exception, the compiler simply does not force you to.