Skip to main content

4.2 Checked vs Unchecked Exceptions

← Back to Chapter 4: Exception Handling

Java is unique among mainstream languages in distinguishing between Checked and Unchecked exceptions at the compiler level.


Difference Between Checked and Unchecked Exceptions​

Checked exceptions are checked by the compiler.

Unchecked exceptions are exceptions that occur during runtime and does not cheched by the compiler.

DimensionChecked ExceptionsUnchecked Exceptions (RuntimeException)
SuperclassDirect subclasses of Exception (except RuntimeException)Subclasses of java.lang.RuntimeException
Compiler EnforcementStrict: Must handle with try-catch OR declare with throwsNone: Compiler does not force handling or declaration
Root CauseUnpredictable external environmental conditionsProgrammer error, violated preconditions, invalid state
Typical ExamplesIOException, SQLException, ParseExceptionNullPointerException, IndexOutOfBoundsException

2. When to Use Each in Real Systems​

When to Use Checked Exceptions:​

  • When a failure is anticipated and the calling code can reasonably recover or provide a fallback.
  • Example: FileNotFoundException—prompting the user to select an alternate file path.

When to Use Unchecked Exceptions:​

  • When the error represents an unrecoverable logic bug or violated contract that requires developer intervention.
  • Example: Passing a negative argument to a method that requires a positive number (IllegalArgumentException).
  • Modern Enterprise Trend (Spring Boot, Hibernate): Modern frameworks wrap low-level checked exceptions into unchecked runtime exceptions (e.g. Spring converts checked SQLException into unchecked DataAccessException) to avoid polluting method signatures with verbose throws clauses.