Extended PHP Exception handling class _php tutorial

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

  • 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.