0
0
PhpHow-ToBeginner · 3 min read

How to Use set_exception_handler in PHP: Simple Guide

Use set_exception_handler in PHP to define a custom function that handles uncaught exceptions globally. This function receives the exception object, letting you log errors or show friendly messages instead of default error output.
📐

Syntax

The set_exception_handler function sets a user-defined function to handle uncaught exceptions. The handler function must accept one parameter: the exception object.

  • set_exception_handler(callable $handler): callable|null
  • $handler: The name of your custom function or a callable that takes the exception as an argument.
  • Returns the previously defined exception handler or null if none was set.
php
set_exception_handler(function (Throwable $exception) {
    // Your code to handle the exception
});
💻

Example

This example shows how to use set_exception_handler to catch an uncaught exception and display a custom message instead of the default error.

php
<?php

// Define a custom exception handler function
function myExceptionHandler(Throwable $exception) {
    echo "Custom Exception Handler:\n";
    echo "Exception message: " . $exception->getMessage() . "\n";
    echo "In file: " . $exception->getFile() . " on line " . $exception->getLine() . "\n";
}

// Set the custom exception handler
set_exception_handler('myExceptionHandler');

// Throw an exception without try-catch to trigger the handler
throw new Exception('Something went wrong!');
Output
Custom Exception Handler: Exception message: Something went wrong! In file: /path/to/script.php on line 15
⚠️

Common Pitfalls

Common mistakes when using set_exception_handler include:

  • Not accepting the exception parameter in the handler function, which causes errors.
  • Throwing exceptions inside the handler itself, which leads to fatal errors.
  • Expecting the handler to catch exceptions inside try-catch blocks; it only catches uncaught exceptions.

Always ensure your handler gracefully handles exceptions without throwing new ones.

php
<?php
// Wrong: handler without parameter
// set_exception_handler(function() {
//     echo "No exception parameter!";
// });

// Right: handler with exception parameter
set_exception_handler(function (Throwable $e) {
    echo "Handled: " . $e->getMessage();
});
📊

Quick Reference

FunctionDescription
set_exception_handler(callable $handler)Sets a global handler for uncaught exceptions.
Exception handler functionReceives the exception object as a parameter.
ReturnsPrevious exception handler or null if none.
UsageUse for logging or showing friendly error messages.

Key Takeaways

Use set_exception_handler to catch all uncaught exceptions globally in PHP.
Your handler function must accept the exception object as a parameter.
Do not throw exceptions inside your exception handler to avoid fatal errors.
set_exception_handler only handles exceptions not caught by try-catch blocks.
Use it to log errors or display user-friendly messages instead of default PHP errors.