In PHP, get_exception_handler() returns the function that currently handles uncaught exceptions, or null if none is set. It needs PHP 8.5 or newer.
You set an exception handler with set_exception_handler(). Until PHP 8.5 there was no direct way to look at it again. get_exception_handler() fixes that. Since it is new, wrap it in function_exists('get_exception_handler') if your code also needs to work on older PHP versions.
Syntax
Call get_exception_handler() with the values you need. Start with this line.
PHP
$handler = get_exception_handler();
Call
What it does
get_exception_handler()
Returns the current exception handler, or null if there is none.
function_exists('get_exception_handler')
Checks that your PHP version supports the function (PHP 8.5+).
set_exception_handler(get_exception_handler())
Sets the same handler again, which is handy when restoring a saved one.
This function needs PHP 8.5 or newer. On older versions, calling it causes a fatal error, so guard it with function_exists(). It only reads the handler and never changes it.
Library declarations
PHP
get_exception_handler(): ?callable
Parameters
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
?callable
The current exception handler exactly as it was given to set_exception_handler(), such as a function name, an array or a Closure. It returns null if no handler is set.
PHP Examples
Example 1 — Reading the Current Handler
At first there is no exception handler. After set_exception_handler(), get_exception_handler() returns its name. This example needs PHP 8.5 or newer.
We register firstHandler and save the current handler in $saved using get_exception_handler().
set_exception_handler('secondHandler') replaces it, and get_exception_handler() reports the new one.
Passing $saved back to set_exception_handler() puts the original handler in place.
get_exception_handler() confirms that firstHandler is active again.
Frequently Asked Questions
It returns the function currently set to handle uncaught exceptions, or null if there is none. It does not change anything.
PHP 8.5 and newer. Older versions do not have it, so check with function_exists('get_exception_handler') before you call it.
It returns null. In that case PHP shows its normal fatal error for an uncaught exception.
They called set_exception_handler() and used the return value, which is the previous handler. That changes the handler, which is why a simple getter is nicer.
No. It only deals with exception handlers. To read the error handler, use get_error_handler(), which is also new in PHP 8.5.