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 not RuntimeException.
  • 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!

๐Ÿ” Navigation

Learn More About Java โ˜•

What is Java and its key features ๐Ÿ‘‰ Explained
Explain the Java Virtual Machine (JVM) ๐Ÿ‘‰ Explained
Difference between JDK, JRE, and JVM ๐Ÿ‘‰ Explained
What are Javaโ€™s main data types ๐Ÿ‘‰ Explained
Explain the concept of Object-Oriented Programming in Java ๐Ÿ‘‰ Explained
What is the difference between a class and an object ๐Ÿ‘‰ Explained
Explain encapsulation with an example ๐Ÿ‘‰ Explained
What is inheritance in Java and its types ๐Ÿ‘‰ Explained
Define polymorphism in Java with examples ๐Ÿ‘‰ Explained
What is abstraction in Java ๐Ÿ‘‰ Explained
Difference between abstract class and interface in Java ๐Ÿ‘‰ Explained
Explain method overloading and method overriding in Java ๐Ÿ‘‰ Explained
What are constructors in Java ๐Ÿ‘‰ Explained
What is the use of the static keyword in Java ๐Ÿ‘‰ Explained
Explain the difference between final, finally, and finalize in Java ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup