What is the difference between checked and unchecked exceptions in Java?
In Java, exceptions are divided into checked and unchecked. The distinction determines whether the compiler forces calling code to handle or declare the exception.
Checked exceptions are subclasses of Exception but not RuntimeException. The compiler enforces that any method which might throw a checked exception must either catch it or declare it with throws. Examples: IOException, SQLException, FileNotFoundException. These represent recoverable conditions — a file that does not exist, a dropped network connection.
Unchecked exceptions are subclasses of RuntimeException. The compiler does not require you to catch or declare them. Examples: NullPointerException, ArrayIndexOutOfBoundsException, IllegalArgumentException. These typically represent programming errors.
Errors (subclasses of Error, like OutOfMemoryError) are also unchecked but represent unrecoverable JVM-level problems.
The class hierarchy: Throwable is the root. It has two subclasses: Exception and Error. Under Exception, RuntimeException and its subclasses are unchecked. Everything else under Exception is checked.
The design rationale for checked exceptions is to force developers to consider failure modes. A method signature like void readFile(String path) throws IOException documents exactly what can go wrong.
However, checked exceptions are controversial. Critics argue they lead to boilerplate (empty catch blocks), poor abstraction (implementation details leaking into signatures), and scaling problems in large codebases. This is why Kotlin, Scala, and Spring predominantly use unchecked exceptions.
Best practices: use checked exceptions for recoverable conditions where the caller can take meaningful action. Use unchecked for programming errors and violated preconditions. Never swallow exceptions silently. When wrapping, preserve the original cause.
| Aspect | Checked Exceptions | Unchecked Exceptions |
|---|---|---|
| Superclass | Exception (not RuntimeException) | RuntimeException |
| Compiler Enforcement | Must catch or declare with throws | No requirement |
| Typical Cause | External/environmental failure | Programming error or bug |
| Examples | IOException, SQLException | NullPointerException, IllegalArgumentException |
| Recoverability | Generally recoverable | Generally indicates a bug to fix |
| API Contract | Part of method signature | Not part of method signature |
Correctly distinguishes based on compiler enforcement and gives examples, but does not discuss the class hierarchy or controversy.
Explains the class hierarchy (Throwable → Exception → RuntimeException), gives concrete examples, discusses when to use each, and addresses the checked exception controversy.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment