Content · Web Development · PHP

Writing Custom Error and Exception Handlers

Central error and exception handlers collect diagnostics without exposing stack traces to end users. Not all fatal errors are recoverable via set_error_handler, and converting warnings to exceptions requires careful error_reporting checks.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Central error and exception handlers collect diagnostics without exposing stack traces to end users.

The examples focus on the operation itself, including the important distinction: try/catch handles expected local failures; global handlers are a final safety net.

What It Does


Use the examples below to observe the behavior on a small dataset before extending it to application code.

Before You Start


Read the input requirements before applying this pattern to production data.

Example 1: Basic Use


Custom error handling should honor error_reporting and avoid publishing raw diagnostics in HTTP output.

<?php
$original = set_error_handler(static function (int $severity, string $message): bool {
    if (!(error_reporting() & $severity)) { return false; }
    throw new ErrorException($message, 0, $severity);
});
try { trigger_error("Unexpected value", E_USER_WARNING); }
catch (ErrorException $e) { echo "Handled: " . $e->getSeverity(); }
finally { restore_error_handler(); }

Output from a successful local PHP 8.4 run:

Handled: 512

Example 2: Checking Behavior


Custom error handling should honor error_reporting and avoid publishing raw diagnostics in HTTP output. Different user-level error severities reach the handler.

<?php
$original = set_error_handler(static function (int $severity, string $message): bool {
    if (!(error_reporting() & $severity)) { return false; }
    throw new ErrorException($message, 0, $severity);
});
try { trigger_error("Unexpected value", E_USER_NOTICE); }
catch (ErrorException $e) { echo "Handled: " . $e->getSeverity(); }
finally { restore_error_handler(); }

Output from a successful local PHP 8.4 run:

Handled: 1024

Example 3: Practical Example


Custom error handling should honor error_reporting and avoid publishing raw diagnostics in HTTP output. Different user-level error severities reach the handler.

<?php
$original = set_error_handler(static function (int $severity, string $message): bool {
    if (!(error_reporting() & $severity)) { return false; }
    throw new ErrorException($message, 0, $severity);
});
try { trigger_error("Unexpected value", E_USER_DEPRECATED); }
catch (ErrorException $e) { echo "Handled: " . $e->getSeverity(); }
finally { restore_error_handler(); }

Output from a successful local PHP 8.4 run:

Handled: 16384

Important Behavior and Edge Cases


Not all fatal errors are recoverable via set_error_handler, and converting warnings to exceptions requires careful error_reporting checks.

Comparison with Related Approaches


ApproachDifference
Writing Custom Error and Exception HandlersCentral error and exception handlers collect diagnostics without exposing stack traces to end users.
Related API or alternativetry/catch handles expected local failures; global handlers are a final safety net.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsNot all fatal errors are recoverable via set_error_handler, and converting warnings to exceptions requires careful error_reporting checks.
Confusing it with a related APItry/catch handles expected local failures; global handlers are a final safety net.

Key Takeaways


  • Central error and exception handlers collect diagnostics without exposing stack traces to end users.
  • Not all fatal errors are recoverable via set_error_handler, and converting warnings to exceptions requires careful error_reporting checks.
  • try/catch handles expected local failures; global handlers are a final safety net.

Use Writing Custom Error and Exception Handlers when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

Share this resourceSend this page to someone who may find it useful.