← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Exception Hierarchy (Throwable, Error, Exception)

php PHP 7.0+ Intermediate

TL;DR

PHP 7+ unified exceptions and fatal errors under the Throwable interface — catch Throwable to handle both Error and Exception in one block.

Explanation

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.

Common 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.

Code Examples

✗ Vulnerable
try {
    $result = intdiv($a, 0);
} catch (Exception $e) {
    // Misses DivisionByZeroError — it's an Error, not Exception
    echo "caught";
}
✓ Fixed
try {
    $result = intdiv($a, 0);
} catch (DivisionByZeroError $e) {
    $result = 0; // specific handling
} catch (\TypeError $e) {
    throw new InvalidArgumentException('Numeric values required', 0, $e);
} catch (\Throwable $t) {
    // Last resort — log everything
    logger()->critical('Unhandled', ['exception' => $t]);
    throw $t;
}

Added 22 Mar 2026
Views 31
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S
No pings yesterday
Amazonbot 8 Perplexity 6 Unknown AI 3 ChatGPT 2 Google 2 SEMrush 2 Meta AI 1 Ahrefs 1
crawler 22 crawler_json 1 pre-tracking 2
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Use catch (\Throwable $t) to catch both Error and Exception — or catch specific subclasses (TypeError, DivisionByZeroError) for precise handling.
📦 Applies To
PHP 7.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
catch (Exception
Auto-detectable: ✓ Yes phpstan psalm
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✗ Manual fix Fix: Low Context: Function Tests: Update
CWE-390 CWE-755

✓ schema.org compliant