In PHP I have built an error handler using set_exception_handler
function. It is executing my custom exception handler. I want PHP to execute default exception handler too after executing my handler. Here is my code.
function handleException( exception $e){
echo $e->getMessage();
restore_exception_handler();
}
set_exception_handler( 'handleException');
echo $e->getMessage()
is executed but then the default PHP exception handler doesn't get executed even after using restore_exception_handler
. So, How can I make it works?
you should trigger previous exception handler after restoring it
function handleException( exception $e){
echo $e->getMessage();
restore_exception_handler();
throw $e; //This triggers the previous exception handler
}
set_exception_handler( 'handleException');
The manual is clear about that:
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.
There is no chance to run any code after that.
But however, you can just call it explicitly:
try {
// some code that throws an Exception
} catch(Exception $e) {
handleException($e);
// .. run custom handler now
}
No need to use restore_exception_handler()
here.