PHP Error Handling set_exception_handler() Function
The PHP Error Handling set_exception_handler() function is used to handle exceptions. If an uncaught exception occurs, you can use it to define a function that will be called. This helps you manage errors more effectively by giving you control over how exceptions are recorded or displayed. Instead of using the normal error message, you can create a more friendly response.
You can use this function to generate error logs or set off alarms when something goes wrong with your code. The set_exception_handler() function optimizes your website's error handling. It improves your application's security and stability.
Syntax
Below is the syntax of the PHP Error Handling set_exception_handler() function −
Callable set_exception_handler ( ?callable $callback )
Parameters
This function accepts $callback parameter which is the function that will be run in the case of an uncaught exception. This handler function just has to accept the Throwable object that was thrown as an argument. Error and Exception both implement the Throwable interface.
Here is the signature for the $callback function −
void handler ( Throwable $ex )
Here $ex is the variable that represents the exception or error that was thrown.
Return Value
The set_exception_handler() function returns null if the previously selected exception handler or an error occurs. If no previous handler was defined, null is also returned.
PHP Version
First introduced in core PHP 5, the set_exception_handler() function continues to function easily in PHP 7, and PHP 8.
Example 1
This simple program has a basic exception handler setup to identify exceptions and display the error message with the help of the PHP Error Handling set_exception_handler() function. When an exception occurs, the handler is called to show the message.
<?php
// Set an exception handler
set_exception_handler(function($ex) {
echo "Exception caught: " . $ex->getMessage();
});
// Throw an exception
throw new Exception("Something went wrong!");
?>
Output
Here is the outcome of the following code −
Exception caught: Something went wrong!
Example 2
In the below PHP code we will use the set_exception_handler() function and set an exception handler that logs the exception message to a file. It helps to keep track of errors without showing them to users.
<?php
// Set an exception handler to log errors
set_exception_handler(function($ex) {
// Log the exception message to a file
file_put_contents('error_log.txt', $ex->getMessage() . "\n", FILE_APPEND);
echo "Error has been logged.";
});
// Throw an exception
throw new Exception("Database connection failed!");
?>
Output
This will generate the below output −
Error has been logged.
Example 3
This program sends an email to the admin whenever an exception occurs with the help of the set_exception_handler() function. It helps in critical situations to notify the concerned person about the error immediately.
<?php
// Set an exception handler to send an email alert
set_exception_handler(function($ex) {
// Send an email with the exception details
mail("admin@example.com", "Error Alert", $ex->getMessage());
echo "Error alert has been sent to the admin.";
});
// Throw an exception
throw new Exception("Critical system failure!");
?>
Output
This will create the below output −
Error alert has been sent to the admin.