Checked vs Unchecked Exceptions: What is the difference?

Short Answer:

Checked exceptions are exceptions that you have to catch with an explicit try/catch block if they could be thrown. Typically, these are common errors that you should be able to know how to handle or recover from.

Unchecked exceptions are exceptions that do not have to be caught by an explicit try/catch block. These typically represent unrecoverable errors or unforseen/unplannable exceptions.

Complicated Answer:

A checked exception is any subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.

Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by’s read() method

Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked.

With an unchecked exception, however, the compiler doesn’t force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String’s charAt() method.

Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be, as they tend to be unrecoverable.


What's Your Opinion?