Engine Exceptions — Error Hierarchy (PHP 7.0)
TL;DR
PHP 7.0 made fatal engine errors catchable as Error subclasses — TypeError, ParseError, ArithmeticError, DivisionByZeroError — via the shared Throwable interface.
Explanation
PHP 7.0 introduced the Error class (distinct from Exception) implementing Throwable. Engine errors that previously caused uncatchable fatals now throw Error subclasses: ParseError (eval syntax errors), TypeError (type declaration violations), ArithmeticError / DivisionByZeroError (math errors), AssertionError (assert() failures), Error (generic engine errors). catch (\Throwable $t) catches everything. catch (\Error $e) catches engine errors only. This unified PHP's error handling — no more register_shutdown_function() to detect fatal errors that were actually TypeError.
Common Misconception
✗ catch (Exception $e) catches PHP 7 engine errors — it doesn't. TypeError, ParseError etc. extend Error not Exception. Use catch (\Throwable $t) for both.
Why It Matters
PHP 7 engine exceptions transformed PHP error handling — previously fatal errors now have a recovery path via catch, enabling more resilient applications.
Common Mistakes
- Catching Exception when TypeError/Error should also be caught.
- Not knowing that each Error subclass can be caught specifically.
- Missing the Throwable interface — it's what unifies Error and Exception.
Code Examples
✗ Vulnerable
try {
$result = intdiv(10, 0);
} catch (Exception $e) {
// Never reached! DivisionByZeroError extends Error, not Exception
}
✓ Fixed
try {
$result = intdiv(10, 0);
} catch (DivisionByZeroError $e) {
$result = 0; // Specific
} catch (\Error $e) {
throw new AppException('Engine error', previous: $e);
} catch (\Throwable $t) {
// Catch-all for both Error and Exception
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
21
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
Amazonbot 1
Amazonbot 10
Unknown AI 3
Google 2
Perplexity 2
ChatGPT 1
Ahrefs 1
Also referenced
How they use it
crawler 16
crawler_json 1
pre-tracking 2
Related categories
⚡
DEV INTEL
Tools & Severity
🟡 Medium
⚙ Fix effort: Low
⚡ Quick Fix
Replace catch(Exception) with catch(\Throwable) for broad catches. Use specific Error 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: Medium
✓ Auto-fixable
Fix: Low
Context: Function
Tests: Update
CWE-390
CWE-755