Checked vs Unchecked Exceptions in Java
Checked exceptions must be handled with `catch` or declared with `throws`, while unchecked exceptions generally represent programming mistakes or invalid runtime state. The distinction is enforced by the Java compiler for checked exceptions.
Java exceptions are objects that represent abnormal conditions during program execution. `Exception` is the main checked-exception family, while `RuntimeException` and its subclasses are unchecked; `Error` is also unchecked but is generally not something application code should recover from.
A checked exception must be accounted for at compile time. If a method can throw one, the caller must either catch it with `try`/`catch` or declare it using `throws`. Common examples include `IOException` and `SQLException`.
Unchecked exceptions do not have this compiler requirement. They commonly indicate defects or invalid assumptions, such as passing `null` where a value is required, accessing an invalid array index, or using an illegal argument. Examples include `NullPointerException`, `IllegalArgumentException`, and `IndexOutOfBoundsException`.
The distinction is mainly about compiler-enforced handling, not whether an exception can occur at runtime. Both categories can propagate through the call stack, and both can be caught explicitly. Good API design chooses checked exceptions when callers are reasonably expected to recover or take an alternate action, and unchecked exceptions when the condition usually indicates a programming error or invalid use of an API.
Key points
- Checked exceptions must be caught or declared with `throws`.
- Unchecked exceptions include `RuntimeException` subclasses and do not require compiler-enforced handling.
- `IOException` is checked; `NullPointerException` and `IllegalArgumentException` are unchecked.
- Both checked and unchecked exceptions can be caught and can propagate up the call stack.
- The choice should reflect whether callers can reasonably recover from the condition.
Interview questions
Does an unchecked exception have to be declared with `throws`?
No. A method may throw an unchecked exception without declaring it in its method signature. The exception can still be documented or declared for clarity, but the compiler does not require that declaration. Checked exceptions, in contrast, must be caught or declared.
What is the difference between checked and unchecked exceptions in Java?
Checked exceptions are checked by the compiler and must be caught or declared with `throws`. Unchecked exceptions are subclasses of `RuntimeException` and do not have that requirement. Checked exceptions often represent conditions a caller can reasonably respond to, such as an I/O failure. Unchecked exceptions commonly indicate invalid input, broken assumptions, or programming defects.
Give examples of checked and unchecked exceptions.
`IOException` and `SQLException` are common checked exceptions. `NullPointerException`, `IllegalArgumentException`, and `IndexOutOfBoundsException` are common unchecked exceptions. The important distinction is their position in Java's exception hierarchy and whether the compiler requires handling.
When would you choose a checked exception for an API?
A checked exception can make sense when the caller is expected to recognize the condition and has a meaningful recovery or alternative action. For example, an operation involving external I/O may fail for reasons the application can reasonably handle. Checked exceptions also make that possibility visible in the method contract through the compiler. They should not be used simply to force callers into boilerplate handling.
Can you catch both checked and unchecked exceptions with the same syntax?
Yes. Both can be handled with `try`/`catch`. For example, `catch (IOException e)` handles a checked exception, while `catch (IllegalArgumentException e)` handles an unchecked exception. The difference is primarily the compiler requirement before runtime, not the syntax used to catch them.