Sobes.tech
Junior — Middle

What is the inheritance hierarchy structure of exceptions in Java?

sobes.tech AI

Answer from AI

In Java, the exception hierarchy is built from the base class java.lang.Throwable. It 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.

Further, Exception is divided into:

  • Checked exceptions — subclasses of Exception except RuntimeException. They need to be either handled or declared in throws.
  • Unchecked exceptions — subclasses of RuntimeException. They do not need to be declared or necessarily handled.

An example hierarchy:

java.lang.Object
 └─ java.lang.Throwable
      ├─ java.lang.Error
      └─ java.lang.Exception
           ├─ java.lang.RuntimeException
           └─ Other checked exceptions (e.g., IOException, SQLException)

Thus, the exception inheritance structure in Java allows for distinguishing system errors from exceptions that may occur during program execution and need to be handled.