• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Programs encounter unexpected situations — file not found, network down, invalid input, out of memory. Without a structured error handling mechanism, every method would need to check and propagate error codes manually, cluttering business logic and making error paths inconsistent and incomplete.

Core Idea

Java’s exception hierarchy is rooted in Throwable, with two main branches: Exception (recoverable conditions) and Error (serious JVM problems). Exceptions are further divided into checked (must be handled or declared) and unchecked (RuntimeException, may occur anywhere).

How It Works

When an exceptional condition occurs, the JVM (or user code) creates an exception object and “throws” it. The runtime searches the call stack for a matching catch block. If none is found, the thread terminates. Checked exceptions are enforced at compile time — the compiler verifies they are handled or declared.

Visual Explanation

java_exceptions Throwable java.lang.Throwable Error Error (Serious JVM issues) OutOfMemoryError StackOverflowError Throwable->Error Exception Exception (Recoverable) Throwable->Exception Checked Checked Exception IOException SQLException ClassNotFoundException Exception->Checked Runtime RuntimeException (Unchecked) NullPointerException ArrayIndexOutOfBounds IllegalArgumentException Exception->Runtime

Semantic Network

semantic_exception_hierarchy THIS Exception Hierarchy TC Try-Catch-Finally THIS--TC builds into TT Throw and Throws THIS--TT builds into CUST Custom Exceptions THIS--CUST builds into NPE NullPointerException THIS--NPE related

Key Properties

  • Throwable root: Only Throwable subclasses can be thrown and caught
  • Checked exceptions: Must be caught or declared in the method signature (throws)
  • RuntimeException: Not checked — can be ignored (programmer error: null checks, bounds checks)
  • Error: Not meant to be caught (JVM in trouble)

Connections

Edge Cases & Gotchas

  • Checked exception abuse: Over-declaring checked exceptions couples callers to implementation details
  • Catching Exception: Catches RuntimeException too — can hide bugs
  • Exception swallowing: Empty catch blocks silently discard errors
  • Finally vs return: finally block executes even if try has a return statement