Java / Java 21 Coding Standards Interview Questions
What is the difference between checked and unchecked exceptions under Java coding standards?
| Checked exception | Unchecked exception |
| Extends Exception (not RuntimeException); the compiler forces callers to catch or declare it. | Extends RuntimeException; the compiler does not require handling. |
| Used for recoverable, expected failures the caller can reasonably act on. | Used for programming errors or conditions the caller cannot meaningfully recover from. |
| Example: IOException from a file read. | Example: IllegalArgumentException from a bad method call. |
The coding standard for choosing between them is based on recoverability, not severity: if the immediate caller has a genuine, sensible recovery path - retrying, falling back to a default, prompting the user again - a checked exception documents that possibility explicitly in the method signature.
If the failure signals a bug or an unrecoverable state, standards favor an unchecked exception, because forcing every caller up a long stack to catch or declare something they cannot actually act on just adds boilerplate without adding safety.
More Related questions...