PHP exception handling function execution sequence is: exception object constructor set_exception_handler() calls shutdown function exit() calls
Function execution in PHP exception handling Sequence
In PHP, when an exception occurs, PHP will execute the following functions in the following order:
Practical case:
<?php try { throw new Exception('My Exception'); } catch (Exception $e) { echo 'Caught exception: ' . $e->getMessage() . PHP_EOL; } finally { echo 'Finally block executed' . PHP_EOL; }
Execution sequence:
Exception
Constructor. set_exception_handler()
handler, skip this step because it is not set here. Output:
Caught exception: My Exception Finally block executed
It is worth noting that even if the exception is thrown in the finally
block, it will not be caught .
The above is the detailed content of What is the order of function execution in PHP exception handling?. For more information, please follow other related articles on the PHP Chinese website!