Extended PHP exception handling class _ PHP Tutorial

Source: Internet
Author: User
Tags php exception handling
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.