What is the order of function execution in PHP exception handling?

王林
Release: 2024-04-17 15:54:01
Original
1036 people have browsed it

PHP exception handling function execution sequence is: exception object constructor set_exception_handler() calls shutdown function exit() calls

PHP 异常处理中的函数执行顺序如何?

Function execution in PHP exception handling Sequence

In PHP, when an exception occurs, PHP will execute the following functions in the following order:

  1. Exception object constructor: Exception object will be created and passed to the constructor.
  2. set_exception_handler() Call: This function will be called if an exception handler has been set.
  3. shutdown function: All registered functions and variables will be destroyed.
  4. exit() call: The script will exit with status code 1.

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;
}
Copy after login

Execution sequence:

  1. Create an exception object and pass it to Exception Constructor.
  2. Call set_exception_handler() handler, skip this step because it is not set here.
  3. All registered functions and variables will be destroyed.
  4. The script will exit with status code 1.

Output:

Caught exception: My Exception
Finally block executed
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template