PHP5 adds exception handling modules similar to other languages. Exceptions in PHP code can be thrown by throw statements and captured by catch statements. PHP 5 adds an exception handling module similar to other languages. Exceptions in PHP code can be thrown by throw statements and captured by catch statements. All codes that require exception handling must be placed in the try code block to capture possible exceptions. Each try must have at least one catch corresponding to it. Multiple catch methods can capture exceptions generated by different classes. When the try code block does not throw an exception or the catch code cannot be found to match the Exception thrown, the PHP code will continue to be executed after the jump to the last catch. Of course, PHP allows another throw exception to be thrown in the catch code block.
When an exception is thrown, subsequent code (the code block where the exception is thrown) will not continue to be executed, PHP will try to find the first catch that can match it. If an Exception is not captured and set_exception_handler () is not used for corresponding processing, PHP will generate a serious error and output the Uncaught Exception... (No exception is captured.
Note that HP internal functions mainly use error reports, and only modern object-oriented extensions use exceptions. However, errors can be easily converted to exceptions through ErrorException.
The PHP Standard Library (SPL) provides many built-in exception classes.
Example #1 throw an exception
getMessage(), "\n"; } // Continue execution echo 'Hello World';?>
The above routine will output:
0.2
Caught exception: Division by zero.
Hello World
Example #2 Nested exceptions
getMessage()); } } } $foo = new Test; $foo->testing();?>
The above routine will output:
String (4) "foo! "