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
| Approach | Difference |
|---|---|
| Writing Custom Error and Exception Handlers | Central error and exception handlers collect diagnostics without exposing stack traces to end users. |
| Related API or alternative | try/catch handles expected local failures; global handlers are a final safety net. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Not all fatal errors are recoverable via set_error_handler, and converting warnings to exceptions requires careful error_reporting checks. |
| Confusing it with a related API | try/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.