Checked vs Unchecked Exceptions in Java
Understand checked vs unchecked exceptions in Java: the hierarchy, compile-time rules, real examples, and when to catch, declare or prevent them.
Expected Interview Answer
Checked exceptions are checked by the compiler and must be either caught or declared with throws, while unchecked exceptions (subclasses of RuntimeException) are not verified at compile time and usually signal programming bugs.
Checked exceptions extend Exception (but not RuntimeException) and represent recoverable conditions outside the program's control, such as a missing file (IOException) or a database failure (SQLException); the compiler forces you to handle them. Unchecked exceptions extend RuntimeException and represent programming errors like NullPointerException, ArrayIndexOutOfBoundsException, or IllegalArgumentException, which are better prevented in code than caught. Errors (such as OutOfMemoryError) are a separate unchecked category representing serious conditions you normally should not catch.
- Checked: compiler forces handling of foreseeable recoverable failures
- Checked: documents failure modes in method signatures
- Unchecked: keeps signatures clean for programmer errors
- Unchecked: avoids clutter for bugs that should be fixed, not caught
- Clear separation of recoverable conditions from coding mistakes
AI Mentor Explanation
A checked exception is like the umpire requiring both teams to sign the match sheet before play — a rule enforced up front that you cannot skip. An unchecked exception is a batter tripping over their own bat: nobody could pre-clear it and it only shows up mid-play as a mistake to fix, not a form to file beforehand.
Step-by-Step Explanation
Step 1
Locate the hierarchy
Throwable splits into Error and Exception; Exception splits into RuntimeException (unchecked) and everything else (checked).
Step 2
Identify checked
Anything extending Exception but not RuntimeException, like IOException, is checked and enforced by the compiler.
Step 3
Identify unchecked
Anything extending RuntimeException, like NullPointerException, is unchecked and not verified at compile time.
Step 4
Handle or declare checked
For checked exceptions, either wrap the risky call in try/catch or add throws to the method signature.
Step 5
Prevent unchecked
For unchecked exceptions, fix the underlying bug (null checks, bounds validation) rather than routinely catching them.
What Interviewer Expects
- The exception class hierarchy (Throwable, Error, Exception, RuntimeException)
- Compile-time enforcement of checked exceptions
- Concrete examples of each category
- When to declare throws vs catch
- That unchecked exceptions usually indicate bugs
Common Mistakes
- Calling NullPointerException a checked exception
- Confusing Error with Exception
- Catching Exception broadly and swallowing it
- Declaring throws for RuntimeException unnecessarily
- Thinking checked exceptions are always slower or worse
Best Answer (HR Friendly)
“Checked exceptions are problems Java forces you to plan for, like a file that might be missing, so the code will not compile until you handle them. Unchecked exceptions are usually programming mistakes, like using something that is empty, that show up while the program runs and should be fixed in the code.”
Code Example
import java.io.*;
// Checked: compiler requires throws or try/catch
void readFile(String path) throws IOException {
BufferedReader r = new BufferedReader(new FileReader(path));
r.readLine();
r.close();
}
// Unchecked: no throws needed, signals a bug
int length(String s) {
return s.length(); // throws NullPointerException if s is null
}
// Handling a checked exception
try {
readFile("data.txt");
} catch (IOException e) {
System.out.println("Could not read: " + e.getMessage());
}Follow-up Questions
- Where do Error types like OutOfMemoryError fit in the hierarchy?
- Should you convert checked exceptions into unchecked ones, and when?
- What is exception chaining and why use the cause constructor?
- Why is catching Throwable considered bad practice?
- How do try-with-resources statements simplify checked exception handling?
MCQ Practice
1. Which of these is an unchecked exception?
NullPointerException extends RuntimeException, so it is unchecked; the others extend Exception and are checked.
2. What must you do with a checked exception?
The compiler requires checked exceptions to be either handled in try/catch or declared using the throws clause.
3. Which class is the superclass of all unchecked exceptions (excluding Errors)?
Unchecked exceptions extend RuntimeException, which itself extends Exception.
Flash Cards
What is a checked exception? — One the compiler forces you to catch or declare; extends Exception but not RuntimeException (e.g. IOException).
What is an unchecked exception? — A RuntimeException subclass not verified at compile time, usually a bug (e.g. NullPointerException).
Where do Errors sit? — Error is a separate Throwable branch for serious conditions like OutOfMemoryError; unchecked and normally not caught.
Handle or declare? — Checked exceptions must be caught or declared with throws; unchecked ones need neither.