What is the difference between a checked and unchecked exceptions?

In Java, exceptions are divided into two main categories: checked and unchecked exceptions. Here’s a clear breakdown to help you grasp the difference:

✅ Checked Exceptions

  • These are checked at compile-time.
  • Java forces you to either handle them using try-catch or declare them using throws.
  • They represent recoverable conditions, like file not found, network issues, or database access errors.

Example:

Common checked exceptions:

  • IOException
  • SQLException
  • FileNotFoundException

⚠️ Unchecked Exceptions

  • These are not checked at compile-time—they occur at runtime.
  • Java does not require you to handle them explicitly.
  • They typically represent programming bugs, like logic errors or improper use of APIs.

Example:

Common unchecked exceptions:

🧠 Quick Summary:

FeatureChecked ExceptionsUnchecked Exceptions
Checked at compile-time?YesNo
Requires handling?Yes (try-catch or throws)No handling required
Extends which class?Exception (but not RuntimeException)RuntimeException
ExamplesIOException, SQLExceptionNullPointerException, ArithmeticException

Think of checked exceptions like seatbelts—you must buckle up before driving (compiling). Unchecked exceptions are more like potholes—Java won’t stop you, but you might hit one if you’re not careful.

Leave a Reply

Your email address will not be published. Required fields are marked *