Junior — Middle
Explain the exception hierarchy structure in programming languages.
sobes.tech AI
Answer from AI
In Java, the exception hierarchy is built on the class java.lang.Throwable, which has two main subclasses:
Error— errors that are usually not handled by the application (e.g.,OutOfMemoryError).Exception— exceptions that can and should be handled.
The Exception class is divided into:
- Checked exceptions — require mandatory handling or declaration in the method (
IOException,SQLException). - Unchecked exceptions — descendants of
RuntimeException, do not require mandatory handling (NullPointerException,IllegalArgumentException).
Thus, the structure looks like this:
Throwable
├── Error
└── Exception
├── RuntimeException (unchecked)
└── Other exceptions (checked)
This allows the compiler and developer to understand which exceptions need to be explicitly handled and which do not.