โšก Track 04 โ€” Mid-Level Masterclass

Advanced Exception Architecture & Exception Chaining

Design resilient error handling with custom domain exceptions, explicit exception chaining (`raise NewError from original_err`), and modern Python 3.11+ Exception Groups (`except*`).

๐Ÿ‘๏ธ Views: 404 Page Views
โšก Mid-Level Python Interactive Studio

Interactive Sandbox Studio: 4 Tracks & 26 Competencies

๐Ÿ‘๏ธ 404 Total Page Views

Switch tracks and select any of the 26 core topics below to run production Python code, inspect CPython memory mechanics, and execute unit test assertion suites in real time.

๐Ÿ“Œ Select Topic (26 Total): Showing 1 Topic for Track 04
โšก Topic 26 of 26 โฑ๏ธ 5 min interactive
Module 26 of 26

26. Custom Exception Hierarchy & Exception Chaining

Create custom domain exceptions by inheriting from Exception. Use raise NewError(...) from orig_err to preserve original tracebacks cleanly.

๐Ÿ’ก Core Engineering Principles

  • Domain Exceptions: Subclass Exception to define domain-specific errors.
  • Explicit Chaining: raise ... from e sets __cause__ to link causal errors.
  • Implicit Chaining: Automatic __context__ association during exception handling.
middle_exceptions_chaining.py
Ready (Python 3.13 Active)
๐Ÿ–ฅ๏ธ Execution Output & Test Results stdout
=== EXECUTION RESULT ===

Caught Domain Error: Failed to connect to primary DB cluster
Original Cause: Socket connection timed out after 5000ms
โœ… Test Passed: Explicit exception chaining linked DatabaseConnectionError to TimeoutError!
โšก 30-Second Developer Summary

Production Error Management Principles

  • Preserve Traceback Context: Explicit exception chaining (`raise CustomError(...) from e`) populates `__cause__`, retaining low-level error stack traces without leaking implementation details to API callers.
  • Domain-Specific Hierarchy: Inherit all application exceptions from a base `AppError(Exception)` to allow callers to catch high-level domain failures cleanly.

1. Designing Custom Exception Hierarchies

A well-designed exception hierarchy groups errors by domain, enabling callers to selectively catch coarse or fine-grained exceptions.

class ApplicationError(Exception):
    """Base exception for all application errors."""
    pass

class DatabaseConnectionError(ApplicationError):
    """Raised when database host fails to respond."""
    pass

class DataValidationError(ApplicationError):
    """Raised when inbound JSON payload violates domain constraints."""
    pass

try:
    raise DataValidationError("Field 'email' is required.")
except ApplicationError as err:
    print(f"Caught Application Level Error: {type(err).__name__} -> {err}")

2. Explicit Exception Chaining (`raise ... from ...`)

Use `raise HighLevelError(...) from low_level_err` to transform technical errors into user-friendly domain errors while preserving original debugging context in `__cause__`.

import json

def parse_user_config(raw_json: str) -> dict:
    try:
        return json.loads(raw_json)
    except json.JSONDecodeError as err:
        # Explicitly chain JSONDecodeError into DataValidationError
        raise DataValidationError("Failed to parse configuration JSON payload") from err

try:
    parse_user_config("invalid_json_str{")
except DataValidationError as exc:
    print("Handled Exception:", exc)
    print("Underlying Root Cause:", repr(exc.__cause__))

3. Exception Groups & `except*` (Python 3.11+)

Python 3.11 introduced `ExceptionGroup` and `except*` to handle multiple concurrent failures simultaneously (e.g. parallel asyncio tasks or batch operations).

# Python 3.11+ Exception Group Example
eg = ExceptionGroup(
    "Batch Task Failures",
    [
        ValueError("Invalid User ID"),
        TypeError("Expected string, got int"),
        KeyError("Missing 'auth_token' key")
    ]
)

try:
    raise eg
except* ValueError as eg_val:
    print("Handled ValueErrors:", eg_val.exceptions)
except* (TypeError, KeyError) as eg_type:
    print("Handled Type & Key Errors:", eg_type.exceptions)

โญ Rate Track 04: Advanced Exception Architecture (1 to 5 Stars)

Your rating is stored directly in the database. Average: 5.0 / 5 (1 rating)

โ˜… โ˜… โ˜… โ˜… โ˜…
โ† Track 03: Memory & Object Model Return to Mid-Level Hub โ†’