Extends the PHP exception handling class. PHP5 adds exception handling modules similar to other languages. Exceptions in PHP code can be thrown by throw statements and captured by catch statements. For all codes that require exception handling, 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.
PHP 5 provides basic exception handling classes that can be used directly.
Throw an error message through an exception:
try { $error = 'my error!'; throw new Exception($error)} catch (Exception $e) { echo $e->getMessage();}
We can expand this class to facilitate our use:
Class MyException extends Exception {// redefine the constructor to change the message to the public function _ construct ($ message, $ code = 0) attribute that must be specified) {// custom code // ensure that all variables are correctly assigned parent ::__ construct ($ message, $ code );} // custom string output style public function _ toString () {return _ CLASS __. ": [{$ this-> code}]: {$ this-> message} \ n";} public function customFunction () {echo "A Custom function for this type of exception \ n ";}}
This class can be expanded as needed.
Http://www.bkjia.com/PHPjc/752479.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752479.htmlTechArticlePHP 5 added exception handling modules 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...