Exception Hierarchy (Throwable, Error, Exception)
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;
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
22 Mar 2026
Views
31
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
No pings yesterday
Amazonbot 8
Perplexity 6
Unknown AI 3
ChatGPT 2
Google 2
SEMrush 2
Meta AI 1
Ahrefs 1
Also referenced
How they use it
crawler 22
crawler_json 1
pre-tracking 2
Related categories
⚡
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