Sobes.tech
Middle+

Describe the exception hierarchy in Java

sobes.tech AI

Answer from AI

The hierarchy of exceptions in Java starts with the base class java.lang.Throwable, which has two main subclasses:

  • Error — errors that are usually not handled by the application (e.g., OutOfMemoryError, StackOverflowError). They indicate serious problems in the JVM.

  • Exception — exceptions that can be handled by the program.

    • Checked exceptions — exceptions that the compiler requires to be either caught or declared in throws. For example, IOException, SQLException. They inherit from Exception, but not from RuntimeException.

    • Unchecked exceptions — inherit from RuntimeException. The compiler does not require them to be handled. Examples include NullPointerException, IllegalArgumentException.

Thus, the main hierarchy looks like this:

java.lang.Object
  └─ java.lang.Throwable
       ├─ java.lang.Error
       └─ java.lang.Exception
            ├─ java.lang.RuntimeException
            └─ other checked exceptions

This separation helps developers understand which exceptions must be handled and which indicate critical runtime errors.