Checked vs Unchecked Exceptions

Java divides exceptions by the compiler rule applied to them. A checked exception must be handled or declared in the method signature. An unchecked exception does not have that requirement. This distinction is about compile-time responsibility, not about whether the failure is important or recoverable.

Checked and unchecked branches of the exception hierarchy

The hierarchy behind the rule

Exception extends Throwable. Its direct and indirect subclasses include both checked and unchecked types. A checked exception is any Exception that is not a RuntimeException. Typical examples are IOException and SQLException. RuntimeException and its subclasses are unchecked; examples include NullPointerException, IllegalArgumentException, and IllegalStateException.

The compiler forces a method that may throw IOException to choose one of two designs: catch it and decide what to do, or declare it with throws IOException. The compiler does not force the same ceremony for IllegalArgumentException, because that type usually represents a violated precondition in the current call. The caller is expected to correct its input or programming logic rather than recover generically.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

static String readFirstLine(Path path) throws IOException {
    return Files.readAllLines(path).getFirst();
}

static int percentage(int value) {
    if (value < 0 || value > 100) {
        throw new IllegalArgumentException("value must be between 0 and 100");
    }
    return value;
}

readFirstLine exposes an external I/O failure to its caller. The caller may show a message, choose a fallback file, or return an appropriate API response. percentage rejects invalid input immediately. The method does not need a checked type because callers cannot meaningfully repair an already invalid argument inside a generic catch block.

Type Compiler requirement Typical meaning
checked exception Catch it or declare it A caller may reasonably recover
IOException Checked File, network, or stream operation failed
SQLException Checked Database access failed at the JDBC boundary
RuntimeException No catch-or-declare rule Invalid state, argument, or programming contract
NullPointerException Unchecked A null reference was used unexpectedly

Choosing a style

Use checked exceptions at a boundary where the caller has a concrete recovery decision and where forcing that decision improves the API. File import code may use IOException because the caller can ask for another file. A library that exposes a low-level resource can also use a checked type when every caller must acknowledge the failure.

Use an unchecked exception for violated method preconditions, invalid domain state, or a failure that should cross several application layers before one central handler translates it. Modern Spring applications commonly use domain-specific RuntimeException types because service methods can propagate them to @RestControllerAdvice without adding throws declarations to every controller and repository-facing method. This does not mean the failure is ignored; it means handling is centralized at the boundary that knows the HTTP contract.

Avoid wrapping every exception in RuntimeException without preserving the cause. Prefer new ImportException("Cannot read import file", ex) so logs retain the original failure. Also avoid choosing a checked type merely because it sounds more serious. The useful question is: which caller can make a meaningful recovery decision?

Glossary
bytecode
Platform-independent compiled code that the JVM can execute.
class
A blueprint that defines fields and methods for objects in object-oriented programming.
object
A runtime instance of a class with its own state and behavior.
method
A named block of behavior that belongs to a class or object and can be called by code.
field
A variable stored inside a class or object that represents part of its state.
constructor
A special method-like block used to create and initialize a new object.
package
A namespace that groups related Java classes and helps organize code.
interface
A contract that defines methods a class can implement without prescribing all internal details.
generics
A language feature that lets code work with types as parameters while preserving type safety.
type erasure
The Java compiler process that removes most generic type details from runtime bytecode.
collection
A data structure that stores a group of elements and provides operations over them.
list
An ordered collection where elements have positions and duplicates are allowed.
set
A collection that stores unique elements and is commonly used for membership checks.
map
A key-value data structure where each key points to a value.
queue
A collection designed for processing elements in a defined order, often first-in-first-out.
lambda
A compact function-like expression used to pass behavior as data.
functional interface
An interface with one abstract method, suitable for lambdas and method references.
stream
A Java API for processing data sequences with operations such as map, filter, and reduce.
optional
A container object that represents a value that may or may not be present.
thread
An independent path of execution inside a running program.
synchronization
Coordination that protects shared data when multiple threads access it.
heap
The runtime memory area where Java objects are allocated.
stack
The runtime memory area that stores method calls, local variables, and call frames.
garbage collector
A JVM component that automatically frees memory occupied by objects no longer reachable.
memory leak
A situation where memory remains occupied because objects are still referenced but no longer useful.