# Checked vs Unchecked Exceptions

> Explain the Java exception hierarchy. What is the difference between checked and unchecked exceptions, and throw vs throws?

Canonical: https://www.internhack.in/learn/interview/java-interview/java-checked-vs-unchecked-exceptions
Last updated: 2026-11-28

Difficulty: Beginner

## Question

Explain the Java exception hierarchy. What is the difference between checked and unchecked exceptions, and throw vs throws?

## Answer

Exceptions are how Java separates the happy path from the failure path, and interviewers use this topic to check whether you write robust code or just wrap everything in a try-catch that prints the stack trace. Let me give you the picture.

At the top of the hierarchy sits Throwable. It has two children. Error represents serious problems in the JVM or environment that your program is not expected to handle, such as OutOfMemoryError, StackOverflowError and NoClassDefFoundError. The other child is Exception, and under it sits RuntimeException.

The classification is straightforward. Checked exceptions are Throwable subclasses that are not RuntimeException or Error. Examples: IOException, SQLException, FileNotFoundException, InterruptedException and ClassNotFoundException. The compiler forces you to deal with them: either catch them or declare them with throws. Unchecked exceptions are RuntimeException and its subclasses (NullPointerException, IllegalArgumentException, ArrayIndexOutOfBoundsException, ArithmeticException, ClassCastException) plus all Errors. The compiler does not force you to handle them.

The idea behind the design: checked exceptions are for recoverable conditions outside your program's control, like a missing file or a network failure, where the caller can reasonably do something such as retry or show a message. Unchecked exceptions signal programming bugs like a null dereference or an invalid argument, which you should fix in code rather than catch.

Now throw versus throws. The keyword throw is a statement that actually raises an exception object: throw new IllegalStateException("bad state"). The keyword throws is part of a method signature, declaring which checked exceptions the method might propagate, so callers know what to handle. Also there is the difference of count: you throw one object at a time, but throws can list several types.

Some rules that come up. When catching multiple exceptions, more specific ones must come before general ones, or the compiler complains about unreachable code. Java 7 introduced multi-catch: catch (IOException | SQLException e). An overriding method cannot throw broader checked exceptions than the method it overrides, though it can throw fewer or any unchecked one. A try block with no possible IOException cannot catch IOException; the compiler reports an error, except for Exception and Throwable.

Best practices worth saying aloud: never swallow exceptions with an empty catch; log with context; do not catch Throwable or Exception blindly; preserve the cause by passing it to the new exception's constructor (exception chaining) so the stack trace shows the root cause; and create custom exceptions extending Exception for recoverable business errors, or RuntimeException when callers cannot do much.

There is real debate too. Many modern frameworks such as Spring wrap checked exceptions into unchecked ones, because checked exceptions clutter lambdas and streams (functional interfaces do not declare them) and encourage catch-and-ignore. Acknowledging this shows maturity. Finally, remember that an Error such as OutOfMemoryError technically can be caught, but there is rarely anything meaningful you can do.

## Code examples

### Handling checked and unchecked exceptions

```
import java.io.IOException;

public class ExceptionDemo {
    static void readFile(String path) throws IOException {
        throw new IOException("missing " + path);
    }

    public static void main(String[] args) {
        try {
            readFile("a.txt");
        } catch (IOException e) {
            System.out.println("Caught: " + e.getMessage());
        }

        try {
            int[] arr = new int[2];
            arr[5] = 1;
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Unchecked: " + e.getMessage());
        }
    }
}
```

readFile must declare throws IOException and the caller must handle it. The array access is unchecked, so the compiler does not require a catch.

## Key points

- Throwable splits into Error and Exception; RuntimeException is the unchecked branch of Exception.
- Checked exceptions are enforced by the compiler; unchecked ones are not.
- throw raises an exception; throws declares possible exceptions in a signature.
- Catch specific exceptions before general ones, and never swallow them silently.
- Use exception chaining to preserve the root cause.

## Common follow-up questions

- Can we catch an Error?
- What is exception chaining?
- Can a constructor or an overriding method throw checked exceptions?
- How do you create a custom exception?

## Interview tips

- Give real examples for each category rather than only definitions.
- Show awareness of the modern view that heavy use of checked exceptions can hurt readability.

## Concepts covered

Exception Hierarchy, Checked, Unchecked, throw vs throws, Error

Previous: [Wrapper Classes, Autoboxing and the Integer Cache](https://www.internhack.in/learn/interview/java-interview/java-wrapper-autoboxing-integer-cache) · Next: [try-with-resources and AutoCloseable](https://www.internhack.in/learn/interview/java-interview/java-try-with-resources) · [All of Core Java & Collections](https://www.internhack.in/learn/interview/java-interview)

## More Core Java & Collections questions

- [The Java Collections Framework Hierarchy](https://www.internhack.in/learn/interview/java-interview/java-collections-hierarchy)
- [ArrayList vs LinkedList](https://www.internhack.in/learn/interview/java-interview/java-arraylist-vs-linkedlist)
- [How HashMap Works Internally](https://www.internhack.in/learn/interview/java-interview/java-hashmap-internals)
- [HashMap vs Hashtable vs ConcurrentHashMap](https://www.internhack.in/learn/interview/java-interview/java-hashmap-hashtable-concurrenthashmap)
- [How HashSet Works Internally](https://www.internhack.in/learn/interview/java-interview/java-hashset-internals)
- [Comparable vs Comparator and How TreeMap Sorts](https://www.internhack.in/learn/interview/java-interview/java-comparable-comparator-treemap)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Core Java & Collections](https://www.internhack.in/learn/interview/java-interview)
