SteelReed

Search

Search posts, questions…

Java CoreQuestion 24 / 43

Junior

What is the difference between checked and unchecked exceptions? Which class should a custom exception extend?

Short answer

A checked exception (like IOException) must be caught or declared with throws, or the code won't compile. Unchecked ones (RuntimeException and subclasses) are not forced.

yesno: business error, bugCreating your ownexceptionCan the caller recover?other file, retryCheckedextends ExceptionUncheckedextends RuntimeExceptionHandled centrally@RestControllerAdvice

3 points to say:

  1. Business errors usually extend RuntimeException.
  2. When wrapping an error, always keep the original (cause).
  3. Don't catch an Error like running out of memory.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.