In PHP, restore_exception_handler() goes back to the exception handler that was active before the most recent call to set_exception_handler().
Exception handlers are kept in a stack, just like error handlers. Each set_exception_handler() puts a handler on top, and restore_exception_handler() takes it off. If there was no earlier handler, uncaught exceptions are handled in PHP's default way again.
Syntax
Call restore_exception_handler() with the values you need. Start with this line.
PHP
restore_exception_handler();
Call
What it does
restore_exception_handler()
Removes the current exception handler and brings back the previous one.
set_exception_handler($fn); /* work */ restore_exception_handler();
Uses a handler for a short piece of code and then removes it.
var_dump(restore_exception_handler())
Prints bool(true), because the function always returns true.
It always returns true, even when no handler was set. It does not catch exceptions. It only changes which handler would run for an uncaught exception.
Library declarations
PHP
restore_exception_handler(): true
Parameters
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
true
Always returns true.
PHP Examples
Example 1 — Going Back to the Previous Handler
Two handlers are set. After restoring, the first one handles the uncaught exception.
set_exception_handler('thirdHandler') returns whatever handler was active just before it.
That handler is firstHandler, which proves the restore worked.
Example 3 — Return Value and the Default Handler
The function returns true. When no handler is left, the next set_exception_handler() returns null.
PHP
<?php
function myHandler(Throwable $e): void {}
set_exception_handler('myHandler');
var_dump(restore_exception_handler());
var_dump(set_exception_handler('myHandler'));
Output
bool(true)
NULL
How It Works
myHandler is registered as the only exception handler.
restore_exception_handler() removes it and returns true.
No handler is active now, so PHP's default behavior is back.
The next set_exception_handler() returns NULL, because there was no previous handler.
Frequently Asked Questions
It removes the current exception handler and brings back the one that was active before it. If there was none, PHP's default behavior for uncaught exceptions is used.
It always returns true.
No. It only changes which function is called for uncaught exceptions. To catch an exception yourself, use try and catch.
Nothing bad happens. The function still returns true and PHP keeps using its default handling.
restore_exception_handler() works on the stack of exception handlers, and restore_error_handler() works on the stack of error handlers. They do not affect each other.