Home>Article>Backend Development> PHP master advanced try catch

PHP master advanced try catch

卡哇伊
卡哇伊 forward
2020-07-18 17:33:18 7796browse

PHP master advanced try catchPHP 5 adds an exception handling module similar to other languages. Exceptions generated in PHP code can be thrown by thethrow statement and captured by the catch statement. (Note: You must throw it first to get it)
Code that requires exception handling must be placed in the try code block to catch possible exceptions.
Each try must have at least one corresponding catch.
Use multiple catches to catch exceptions generated by different classes.
When the try code block no longer throws an exception or no catch can be found to match the thrown exception, the PHP code will continue execution after jumping to the last catch.
Of course, PHP allows exceptions to be thrown again within the catch code block.
When an exception is thrown, the subsequent code (Translator's Note: refers to the code block when the exception is thrown) will not continue to execute, and PHP will try to find the first matching catch.

If an exception is not caught and there is no need to use set_exception_handler() for corresponding processing, then PHP will generate a serious error and output Uncaught Exception... (uncaught exception) prompt information.

Let’s first take a look at the basic properties and methods of PHP’s built-in exception class. (Excluding specific implementation)

The example is as follows:

Include file error throws exception

getMessage(); } // 正确的抛出异常 try { if (file_exists('test_try_catch.php')) { require ('test_try_catch.php'); } else { throw new Exception('file is not exists'); } } catch (Exception $e) { echo $e->getMessage(); }

More tutorials: "php tutorial"

The above is the detailed content of PHP master advanced try catch. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete