Difference between checked and unchecked exceptions in Java
๐ก Concept: Checked vs Unchecked Exceptions
Java exceptions are divided into checked and unchecked based on whether the compiler forces exception handling.
๐ Quick Intro
Checked exceptions must be either caught or declared in the method signature; unchecked exceptions do not require explicit handling.
๐ง Analogy
Think of checked exceptions as mandatory warnings you must address, like a fire alarm, while unchecked exceptions are unexpected breakdowns that might crash your system if unhandled.
๐ง Technical Explanation
- Checked exceptions extend
Exception
but notRuntimeException
. - Unchecked exceptions extend
RuntimeException
. - Checked exceptions require try-catch or throws declaration.
- Unchecked exceptions can be ignored or handled optionally.
- Errors are a separate category, generally unrecoverable.
๐ฏ Use Cases
- โ Checked exceptions: recoverable conditions (e.g., IOException).
- โ Unchecked exceptions: programming errors (e.g., NullPointerException).
- โ Design APIs to clearly indicate exception handling.
๐ป Java Exception Example
public void readFile(String filename) throws IOException {
FileReader file = new FileReader(filename);
// IOException is checked, must declare or catch
}
public void divide(int a, int b) {
int result = a / b; // ArithmeticException unchecked
}

โ Interview Q&A
Q1: What is a checked exception?
A: Exception checked at compile time requiring handling.
Q2: What is an unchecked exception?
A: Exception not checked at compile time.
Q3: Are Errors checked or unchecked?
A: Neither, they indicate serious problems.
Q4: How to handle checked exceptions?
A: Try-catch or throws.
Q5: Can unchecked exceptions be caught?
A: Yes, optionally.
Q6: Example of checked exception?
A: IOException.
Q7: Example of unchecked exception?
A: NullPointerException.
Q8: Is it mandatory to declare unchecked exceptions?
A: No.
Q9: Difference between Exception and Error?
A: Exception is recoverable, Error is not.
Q10: Can RuntimeException be a checked exception?
A: No.
๐ MCQs
Q1. What is a checked exception?
- Unchecked exception
- Checked exception
- Error
- RuntimeException
Q2. What is an unchecked exception?
- Checked exception
- Unchecked exception
- Error
- Throwable
Q3. Are Errors checked or unchecked?
- Checked
- Unchecked
- Neither
- Both
Q4. How to handle checked exceptions?
- Ignore
- Try-catch or throws
- Only throws
- Only try-catch
Q5. Can unchecked exceptions be caught?
- No
- Yes
- Only checked exceptions
- Never
Q6. Example of checked exception?
- NullPointerException
- IOException
- ArithmeticException
- Error
Q7. Example of unchecked exception?
- IOException
- NullPointerException
- FileNotFoundException
- SQLException
Q8. Is it mandatory to declare unchecked exceptions?
- Yes
- No
- Sometimes
- Depends
Q9. Difference between Exception and Error?
- Both recoverable
- Exception is recoverable, Error is not
- Both unrecoverable
- None
Q10. Can RuntimeException be a checked exception?
- Yes
- No
- Sometimes
- Only in Java 9+
๐ก Bonus Insight
Understanding checked vs unchecked exceptions improves error handling and software robustness.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!