Menu
Coddy logo textTech

Exception Class Hierarchy

Part of the Object Oriented Programming section of Coddy's Java journey — lesson 59 of 87.

Java's exception handling system is built on a well-organized class hierarchy rooted in the Throwable class. Understanding this hierarchy helps you catch and handle exceptions more effectively.

At the top sits Throwable, the parent of all exceptions and errors. It has two direct subclasses:

Throwable
├── Error          // Serious problems (don't catch these)
└── Exception      // Recoverable problems
    └── RuntimeException  // Unchecked exceptions

Error represents serious problems that applications shouldn't try to handle—like OutOfMemoryError or StackOverflowError. These indicate fundamental issues with the JVM itself.

Exception is the branch you'll work with most. It splits into two categories: checked exceptions (direct subclasses of Exception) and unchecked exceptions (subclasses of RuntimeException).

// Common exception classes
Exception
├── IOException           // File/network problems
├── SQLException          // Database problems
└── RuntimeException
    ├── NullPointerException
    ├── ArrayIndexOutOfBoundsException
    └── IllegalArgumentException

Because exceptions are classes, they follow inheritance rules. When you catch a parent exception type, you also catch all its children:

try {
    // some code
} catch (Exception e) {
    // Catches ALL exceptions (too broad usually)
}
try {
    // some code
} catch (RuntimeException e) {
    // Catches only runtime exceptions and subclasses
}

This hierarchy lets you be as specific or general as needed when handling errors, and it's the foundation for creating your own custom exceptions.

challenge icon

Challenge

Easy

Let's build an exception analyzer that demonstrates how Java's exception hierarchy works in practice! You'll create a system that catches exceptions at different levels of specificity and reports which type was caught.

You'll organize your code across three files:

  • ExceptionThrower.java: Create a class that can trigger different types of exceptions based on input. Your class should have three static methods:

    triggerRuntime(String type) - Based on the type string, throw different runtime exceptions:

    • If type is "null", throw a NullPointerException with message "Null value encountered"
    • If type is "index", throw an ArrayIndexOutOfBoundsException with message "Invalid index"
    • If type is "argument", throw an IllegalArgumentException with message "Bad argument"

    triggerArithmetic() - Throw an ArithmeticException with message "Division error"

    triggerGeneric() - Throw a RuntimeException with message "Generic runtime error"

  • ExceptionAnalyzer.java: Create a class that catches exceptions at different hierarchy levels and identifies what was caught. Include these static methods:

    analyzeSpecific(String type) - Call ExceptionThrower.triggerRuntime(type) inside a try block. Use separate catch blocks for NullPointerException, ArrayIndexOutOfBoundsException, and IllegalArgumentException. For each, print: Caught specific: [exception class simple name] followed by Message: [exception message]

    analyzeWithParent() - Call ExceptionThrower.triggerArithmetic() inside a try block. Catch it using RuntimeException (the parent type). Print: Caught via parent: RuntimeException followed by Actual type: [exception class simple name]

    analyzeWithGrandparent() - Call ExceptionThrower.triggerGeneric() inside a try block. Catch it using Exception (even higher in the hierarchy). Print: Caught via grandparent: Exception followed by Actual type: [exception class simple name]

  • Main.java: Bring your exception analyzer together! You'll receive one input: an exception type string (either "null", "index", or "argument").

    First, print === Specific Catch === and call analyzeSpecific with your input.

    Then print a blank line, === Parent Catch ===, and call analyzeWithParent.

    Finally, print a blank line, === Grandparent Catch ===, and call analyzeWithGrandparent.

You will receive one input: the exception type to trigger ("null", "index", or "argument").

Use e.getClass().getSimpleName() to get the exception's class name and e.getMessage() to retrieve its message. This challenge shows how catching a parent exception type also catches all its children—a key concept when deciding how specific your exception handling should be!

Cheat sheet

Java's exception handling is built on a class hierarchy rooted in Throwable:

Throwable
├── Error          // Serious problems (don't catch)
└── Exception      // Recoverable problems
    └── RuntimeException  // Unchecked exceptions

Error represents serious JVM problems like OutOfMemoryError or StackOverflowError that applications shouldn't handle.

Exception splits into checked exceptions (direct subclasses) and unchecked exceptions (subclasses of RuntimeException):

Exception
├── IOException           // File/network problems
├── SQLException          // Database problems
└── RuntimeException
    ├── NullPointerException
    ├── ArrayIndexOutOfBoundsException
    └── IllegalArgumentException

Catching a parent exception type also catches all its children:

try {
    // some code
} catch (Exception e) {
    // Catches ALL exceptions
}

try {
    // some code
} catch (RuntimeException e) {
    // Catches only runtime exceptions and subclasses
}

Use e.getClass().getSimpleName() to get the exception's class name and e.getMessage() to retrieve its message.

Try it yourself

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String type = scanner.nextLine();
        
        // TODO: Print "=== Specific Catch ===" and call ExceptionAnalyzer.analyzeSpecific(type)
        
        // TODO: Print a blank line, then "=== Parent Catch ===", then call ExceptionAnalyzer.analyzeWithParent()
        
        // TODO: Print a blank line, then "=== Grandparent Catch ===", then call ExceptionAnalyzer.analyzeWithGrandparent()
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming