Checked vs unchecked
Checked exceptions must be caught or declared in throws. Unchecked exceptions (RuntimeException and Error) do not. The compiler enforces only the first group.
Detailed Explanation
Java splits failures into two kinds. A checked exception is a problem a caller can reasonably be expected to handle: the file is missing, the network reset, the SQL failed. The compiler forces you to write catch or throws. An unchecked exception is usually a programming mistake: a null reference, a bad index, an illegal argument. The compiler stays silent. If you do not catch it, it unwinds the stack.
The type hierarchy is the rule. Throwable has two children: Error and Exception. Error is for the JVM (OutOfMemoryError, StackOverflowError) and is unchecked. Exception is checked, except for RuntimeException and its subclasses, which are unchecked. IOException is checked. IllegalArgumentException is not.
The original idea was documentation you cannot skip. In practice, checked exceptions also force noisy wrappers. A method that calls a JDBC driver cannot declare SQLException if its callers are domain code. People catch, wrap in UncheckedIOException or a custom runtime exception, and rethrow. That is a valid pattern when the caller cannot recover. It is a smell when you write catch (Exception e) { } and continue.
You throw checked when the caller has a useful local response: try another file, retry the socket, show "email already used". You throw unchecked when the call was illegal (null id, negative amount) or when the failure means the request should fail fast. Library authors often prefer unchecked APIs (Files.readString still throws checked IOException; many modern HTTP clients do not).
throws on a method is part of its contract. Adding a checked exception is a source-breaking change. Removing one is not. Overriding methods cannot add new checked exceptions. Lambdas that implement a functional interface cannot throw checked exceptions unless the interface method declares them, which is why Runnable lambdas cannot throw IOException without a wrapper.
The cost of overusing checked exceptions is throws Exception on every method, which hides the ones that matter. The cost of using only unchecked is silent new failure modes at call sites that used to compile. Pick per method. Document both.
Code Examples
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Read {
static String firstLine(Path path) throws IOException { // checked: callers must handle
return Files.readAllLines(path).get(0);
}
static int parsePositive(String raw) {
int n = Integer.parseInt(raw); // NumberFormatException is unchecked
if (n <= 0) {
throw new IllegalArgumentException("must be > 0");
}
return n;
}
public static void main(String[] args) {
System.out.println(parsePositive("3")); // prints: 3
}
}public class Wrap {
static Runnable load(String name) {
return () -> {
if (name.isBlank()) {
// Runnable.run() declares no checked exception, so we throw unchecked.
throw new IllegalStateException("blank name");
}
System.out.println(name);
};
}
public static void main(String[] args) {
load("ok").run(); // prints: ok
}
}Common Gotchas
An uncaught checked exception does not compile
add `throws` or a `catch`. The compiler will not let you "forget".
`catch (Exception e)` also catches unchecked exceptions
you may hide `NullPointerException` and keep running in a bad state.
Wrapping without the cause loses the stack
use `new RuntimeException("read failed", e)` so logs still show the original throw site.
`Error` is unchecked and rarely catchable in a useful way
after `OutOfMemoryError` the JVM may already be dying.