Exceptions generated in PHP code can be thrown by thethrow
statement and captured by thecatch
statement. Code that requires exception handling must be placed within thetry
code block, and eachtry
must have at least one correspondingcatch
. When an exception is thrown, the code following the code block will not continue to execute. At this time, PHP will try to find the first matchingcatch
. Of course, PHP allowsthrow
exceptions to be thrown again within acatch
code block. If an exception is not caught and handled accordingly usingset_exception_handler()
, PHP will generate a fatal error.
Here is an example of exception usage.
'; echo inverse(0) . '
'; } catch(Exception $e) { echo 'Caught exception: ' . $e->getMessage() . '
'; } echo 'hello';
There is also an example of exception nesting.
getMessage()); } } } $foo = new Test; $foo->testing();
Users can extend PHP's built-in exception handling classes with custom exception handling classes.
(Full text ends)
The above introduces the exception handling - PHP manual notes, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.