Java ProgramsExceptionsException Hierarchy

Exception Hierarchy in Java

intermediate·  Exceptions  ·  Error Handling

Problem

Every Java exception descends from Throwable through Exception and, for unchecked ones, RuntimeException — a catch block written for an ancestor type still matches any of its descendants, thanks to ordinary object-oriented polymorphism.

Throw a specific exception type but catch it through a more general ancestor class, then confirm which exact type was actually thrown.

Input
Integer.parseInt("abc")
Output
Caught as: java.lang.NumberFormatException

Java Program

Java
public class ExceptionHierarchyExample { public static void main(String[] args) { try { int n = Integer.parseInt("abc"); } catch (RuntimeException e) { // getName() reports the actual thrown type, not the catch clause's declared type System.out.println("Caught as: " + e.getClass().getName()); } } }

Output

Caught as: java.lang.NumberFormatException

Core Logic

Catching a RuntimeException reference still captures a NumberFormatException object underneath, since a catch clause matches by the exception's actual type against everything in its ancestry, not just an exact name.

How It Works
  1. 1Integer.parseInt("abc") throws a NumberFormatException, since "abc" isn't a valid integer.
  2. 2NumberFormatException extends IllegalArgumentException, which extends RuntimeException — so catch (RuntimeException e) matches it.
  3. 3e.getClass().getName() reports the object's real, most specific class — NumberFormatException — regardless of which ancestor type the catch clause named.
  4. 4The variable e is declared as RuntimeException inside the catch block, but the object it refers to is still, underneath, a full NumberFormatException.
Parsing "abc" throws a NumberFormatException, caught by the RuntimeException clause, and getClass().getName() still reports java.lang.NumberFormatException.
💡

Key Point: This is the same polymorphism rule that applies to any Java class hierarchy — catching by a supertype trades away the ability to distinguish which subtype occurred, in exchange for one catch block that handles the whole family of related exceptions.

Key Concepts

exception hierarchypolymorphismgetClass()

Related Programs