set_exception_handler
(PHP 5, PHP 7)
set_exception_handler — Sets a user-defined exception handler function
Description
Sets the default exception handler if an exception is not caught within a
try/catch block. Execution will stop after the
exception_handler
is called.
Parameters
-
exception_handler
-
Name of the function to be called when an uncaught exception occurs. This handler function needs to accept one parameter, which will be the exception object that was thrown. This is the handler signature before PHP 7:
Since PHP 7, most errors are reported by throwing Error exceptions, which will be caught by the handler as well. Both Error and Exception implements the Throwable interface. This is the handler signature since PHP 7:
NULL
may be passed instead, to reset this handler to its default state.CautionNote that providing an explicit Exception type hint for the
ex
parameter in your callback will cause issues with the changed exception hierarchy in PHP 7.
Return Values
Returns the name of the previously defined exception handler, or NULL
on error. If
no previous handler was defined, NULL
is also returned.
Changelog
Version | Description |
---|---|
7.0.0 |
The type of parameter passed into exception_handler changed
from Exception to Throwable
|
5.5.0 |
Previously, if NULL was passed then this function returned TRUE .
It returns the previous handler since PHP 5.5.0.
|
Examples
Example #1 set_exception_handler() example
<?php
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\n";
}
set_exception_handler('exception_handler');
throw new Exception('Uncaught Exception');
echo "Not Executed\n";
?>
See Also
- restore_exception_handler() - Restores the previously defined exception handler function
- restore_error_handler() - Restores the previous error handler function
- error_reporting() - Sets which PHP errors are reported
- information about the callback type
- PHP 5 Exceptions
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-set-exception-handler.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.