PHP 5 adds exception handling modules similar to those in other languages. Exceptions generated in PHP code can be thrown by a throw statement and captured by a catch statement. The code that requires exception handling must be placed inside a try code block to catch possible exceptions. Each try must have at least one catch corresponding to it. You can use multiple catches to catch exceptions that are generated by different classes. When the try code block no longer throws an exception or cannot find a catch that matches the thrown exception, the PHP code resumes execution after jumping to the last catch. Of course, PHP allows you to throw (throw) exceptions again within a catch code block.
PHP 5 provides a basic exception handling class that can be used directly.
Throw an error message through the exception:
try { $error = ' my error! '; throw new Exception ($error)} catch (Exception $e) { echo $e->getmessage ();}
We can extend this class to facilitate our use of:
Class MyException extends exception{ //redefine the constructor to make the message a property that must be specified public function __construct ($message, $code = 0) { //Custom code //Make sure 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 used as freely as you want.
http://www.bkjia.com/PHPjc/752479.html www.bkjia.com true http://www.bkjia.com/PHPjc/752479.html techarticle PHP 5 adds exception handling modules similar to those in other languages. Exceptions generated in PHP code can be thrown by a throw statement and captured by a catch statement. The code that requires exception handling is ...