Skip to content
Sharp

Exceptions · interview question

Explain checked vs unchecked exceptions in Java. When would you use each in your own API?

Interview answerMedium

0 words · aim for 40+

Answer and explanation

Correct answer

Checked exceptions are subclasses of Exception but not RuntimeException, like IOException. The compiler forces callers to catch or declare them. Unchecked exceptions extend RuntimeException, like IllegalArgumentException and NullPointerException, and don't need to be declared.

Use checked exceptions for recoverable conditions the caller can reasonably handle, such as a missing file the user can pick again. Use unchecked exceptions for programming errors and precondition violations (bad arguments, illegal state) and when the caller can't do anything useful. Many modern APIs lean unchecked because checked exceptions don't compose well with lambdas and streams. Either way, document what you throw, keep the cause when wrapping (new ServiceException("msg", e)), and don't swallow exceptions.

Key points:
- Checked exceptions must be caught or declared; unchecked don't
- Unchecked exceptions extend RuntimeException
- Checked: recoverable conditions the caller can handle
- Unchecked: programming errors and precondition violations
- Preserve the cause when wrapping exceptions

Why

Mentioning lambdas/streams friction and exception chaining shows real-world experience.

References

Test yourself on Exceptions

A free test with instant, explained results. No account needed.

Start free test
All Exceptions questions →