Software Engineer's Blog

Understanding Java Exception Hierarchy

Understanding Java Exception Hierarchy

Java provides a predictable and powerful error-handling mechanism centered around the Throwable hierarchy. Understanding this system is essential not only for writing safer and cleaner code but also for building production-grade applications—especially in modern frameworks like Spring Boot.

This guide walks you through:

  • The Java exception hierarchy
  • Real-world handling of checked vs unchecked exceptions
  • Best practices used in enterprise systems
  • How to design custom exceptions the right way
  • Practical Q&A from a senior developer’s perspective

Java Exception Hierarchy Overview

Throwable
├── Error
│   ├── VirtualMachineError
│   │   ├── StackOverflowError
│   │   ├── OutOfMemoryError
│   │   └── UnknownError
│   ├── IOError
│   ├── AssertionError
│   ├── LinkageError
│   │   └── NoClassDefFoundError
│   └── AnnotationFormatError

└── Exception
    ├── IOException
    │   ├── FileNotFoundException
    │   └── SocketException

    ├── RuntimeException
    │   ├── ArithmeticException
    │   ├── NullPointerException
    │   ├── NumberFormatException
    │   └── IndexOutOfBoundsException
    │       ├── ArrayIndexOutOfBoundsException
    │       └── StringIndexOutOfBoundsException

    ├── SQLException
    └── ClassNotFoundException

1. The Root: Throwable

At the top of Java’s exception model is the Throwable class. It has two primary branches:

  • Error — JVM-level failures (not recoverable)
  • Exception — Application-level issues (recoverable)

2. Error: Critical JVM/System-Level Failures

Error represents internal system failures that applications should not attempt to handle.

ClassDescription
StackOverflowErrorInfinite recursion or deeply nested calls
OutOfMemoryErrorJVM heap exhausted
InternalErrorJVM internal malfunction

Do not catch Error in your application code.
Let the JVM handle it. Attempting recovery often causes more harm.

3. Exception: Recoverable Application-Level Issues

Exception represents problems that commonly occur during normal execution—many of which can be handled or recovered from.

Common subclasses

ClassDescription
IOExceptionI/O failures such as file or network problems
SQLExceptionDatabase access errors
ClassNotFoundExceptionClass not found at runtime
RuntimeExceptionUnchecked exceptions (usually programming errors)

4. Checked vs Unchecked Exceptions

Java differentiates between checked and unchecked exceptions:

✔ Checked Exceptions

  • The compiler forces handling (throws or try-catch)
  • Usually caused by unpredictable external factors
    • File operations
    • Network connections
    • Database access
try {
    BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
} catch (IOException e) {
    System.out.println("Unable to read the file.");
}

✔ Unchecked Exceptions (RuntimeException)

  • Compiler does not require handling
  • Typically caused by programming mistakes
    • Null access
    • Invalid indexing
    • Invalid input
int[] arr = {1, 2, 3};
System.out.println(arr[5]); // ArrayIndexOutOfBoundsException

5. Common RuntimeException Types

ExceptionDescription
NullPointerExceptionDereferencing a null reference
ArithmeticExceptionDivision by zero, etc.
NumberFormatExceptionInvalid numeric string
IndexOutOfBoundsExceptionInvalid index for arrays/lists

Subclasses include:

  • ArrayIndexOutOfBoundsException
  • StringIndexOutOfBoundsException