{
    "slug": "php_exception_hierarchy",
    "term": "Exception Hierarchy (Throwable, Error, Exception)",
    "category": "php",
    "difficulty": "intermediate",
    "short": "PHP 7+ unified exceptions and fatal errors under the Throwable interface — catch Throwable to handle both Error and Exception in one block.",
    "long": "PHP 7 introduced Throwable as the root interface. Exception (user exceptions) and Error (engine errors like TypeError, ParseError, ArithmeticError) both implement it. Before PHP 7, fatal errors like calling undefined functions were uncatchable. Now: catch (\\Throwable $t) catches everything. catch (\\Error $e) catches engine errors. catch (\\Exception $e) catches application exceptions. Key subclasses: TypeError (wrong types), ValueError (invalid argument values), ArithmeticError / DivisionByZeroError, ParseError, Error.",
    "aliases": [],
    "tags": [
        "php",
        "exceptions",
        "error-handling"
    ],
    "misconception": "catch (Exception $e) catches everything — it misses Error subclasses (TypeError, ParseError etc.) which only implement Throwable, not Exception.",
    "why_it_matters": "Understanding the hierarchy prevents silently missing engine errors and enables writing catch blocks that handle exactly the right error class.",
    "common_mistakes": [
        "Catching Exception when Error subclasses are possible — use Throwable.",
        "Not distinguishing between recoverable errors (TypeError from user input) and programming errors (wrong arg type in internal code).",
        "Swallowing Throwable in catch blocks without logging."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "exception_handling",
        "php_error_handler",
        "php_type_error",
        "php_division_by_zero"
    ],
    "prerequisites": [
        "php_error_levels"
    ],
    "refs": [
        "https://www.php.net/manual/en/language.exceptions.php"
    ],
    "bad_code": "try {\n    $result = intdiv($a, 0);\n} catch (Exception $e) {\n    // Misses DivisionByZeroError — it's an Error, not Exception\n    echo \"caught\";\n}",
    "good_code": "try {\n    $result = intdiv($a, 0);\n} catch (DivisionByZeroError $e) {\n    $result = 0; // specific handling\n} catch (\\TypeError $e) {\n    throw new InvalidArgumentException('Numeric values required', 0, $e);\n} catch (\\Throwable $t) {\n    // Last resort — log everything\n    logger()->critical('Unhandled', ['exception' => $t]);\n    throw $t;\n}",
    "quick_fix": "Use catch (\\Throwable $t) to catch both Error and Exception — or catch specific subclasses (TypeError, DivisionByZeroError) for precise handling.",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-22",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_exception_hierarchy",
        "html_url": "https://codeclaritylab.com/glossary/php_exception_hierarchy",
        "json_url": "https://codeclaritylab.com/glossary/php_exception_hierarchy.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Exception Hierarchy (Throwable, Error, Exception)](https://codeclaritylab.com/glossary/php_exception_hierarchy) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_exception_hierarchy"
            }
        }
    }
}