← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

Exception Handling Introduced (PHP 5)

PHP PHP 5.0+ Beginner
debt(d7/e3/b3/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints list phpstan as the tool, but automated is explicitly 'no'. Common mistakes like swallowing exceptions (empty catch blocks) or catching generic Exception instead of specific types are not reliably caught by default linting — phpstan can detect some patterns but requires careful configuration and manual review to catch the full range of misuse. Silent swallowing in production is the worst case.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix describes replacing die()/return false with throw, catching specific types, and re-throwing with context. These are pattern replacements within a component — not single-line trivial fixes, but not cross-cutting architectural rework either. Fixing swallowed exceptions or overly broad catches is a repeated but localised refactor.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). Exception handling applies broadly (web, cli, queue-worker) but the common mistakes (swallowed exceptions, wrong catch granularity) impose a tax primarily on the components where they occur rather than shaping the entire codebase. It does not have strong gravitational pull across the architecture.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field identifies that developers misunderstand finally — believing it only runs after catch, when in fact it runs whether or not an exception was thrown, and even after return in try/catch. This is a documented gotcha that most developers eventually learn, consistent with the t5 anchor. Additionally, swallowing exceptions and using exceptions for flow control are well-known but non-obvious pitfalls.

About DEBT scoring →

TL;DR

PHP 5 introduced try/catch/finally and the Exception class — replacing PHP 4's procedural error handling with structured exception-based patterns.

Explanation

PHP 5 (2004) added: try { } catch (ExceptionType $e) { } finally { }. The Exception base class with getMessage(), getCode(), getFile(), getLine(), getTrace(). Custom exceptions via class MyException extends Exception. PHP 4 had no try/catch — errors were handled with die(), trigger_error(), or return codes. The shift to exceptions enabled frameworks to build proper error handling. PHP 5.1 added set_exception_handler(). PHP 7 unified exceptions and errors under Throwable. Key principle: throw exceptions for exceptional conditions, not for flow control.

Common Misconception

Finally blocks always run after catch — finally runs whether or not an exception was thrown (and even after return in try/catch). It's the cleanup guarantee.

Why It Matters

Structured exception handling replaces PHP 4's fragile die()/return-code patterns with a composable, catchable error propagation system.

Common Mistakes

  • Catching generic Exception when specific subclasses should be caught.
  • Using exceptions for flow control (throwing to exit loops).
  • Swallowing exceptions: catch(Exception $e) {} with empty body.
  • Not using finally for cleanup (DB connections, file handles).

Code Examples

✗ Vulnerable
// PHP 4 style — checking return values:
$result = connectDb();
if ($result === false) { die('DB failed'); }
✓ Fixed
try {
    $pdo = new PDO($dsn, $user, $pass);
} catch (\PDOException $e) {
    throw new DatabaseConnectionException(
        'Could not connect to database',
        previous: $e
    );
} finally {
    // Cleanup always runs
}

Added 23 Mar 2026
Views 93
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping F 0 pings S 0 pings S 2 pings M 1 ping T 1 ping W 3 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 1 ping W 1 ping T 1 ping F 0 pings S
No pings yet today
Amazonbot 1
PetalBot 10 Amazonbot 8 Google 6 Ahrefs 6 SEMrush 6 Scrapy 6 Perplexity 4 Bing 4 Unknown AI 3 Meta AI 2 Applebot 2 ChatGPT 1 Majestic 1 Twitter/X 1 Sogou 1 Brave Search 1
crawler 59 crawler_json 2 pre-tracking 1
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
PHP php A server-side scripting language that generates web pages and APIs — the code runs on the server, and only its output (usually HTML or JSON) reaches the browser.

PHP is often the first server-side language people meet, and understanding its execution model — script starts fresh on every request, no memory between requests — explains most of how the web backend works: sessions, databases, and caching all exist to bridge that per-request amnesia.

💡 Start with PHP 8.x, declare(strict_types=1), and PDO — skip any tutorial that mentions mysql_query().

Ask Codex about PHP →
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Replace die()/return false with throw. Catch specific exception types. Always re-throw with context: throw new MyException('msg', previous: $e).
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
catch.*Exception|try\s*{
Auto-detectable: ✗ No phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Function Tests: Update
CWE-390 CWE-755


✓ schema.org compliant