Extended php built-in exception handling class

Source: Internet
Author: User
Tags php exception handling
You can use a custom exception handling class to extend the PHP built-in exception handling class. The following code illustrates which attributes and methods are accessible and inherited in the built-in exception handling class. You can use a custom exception handling class to extend the PHP built-in exception handling class. The following code illustrates which attributes and methods are accessible and inherited in the built-in exception handling class. Note: The following code only describes the structure of the built-in exception handling class. it is not a practical usable code.

Example #1 built-in exception handling class

 

If you want to use a custom class to extend the built-in exception handling class and redefine the constructor, we recommend that you call parent :__ construct () at the same time to check whether all variables have been assigned values. When an object needs to output a string, you can reload _ toString () and customize the output style.

Example #2 extend the built-in PHP exception handling class

 Code}]: {$ this-> message} \ n ";} public function customFunction () {echo" A Custom function for this type of exception \ n ";}} /*** create a class for testing the exception handling mechanism */class TestException {public $ var; const THROW_NONE = 0; const THROW_CUSTOM = 1; const THROW_DEFAULT = 2; function _ construct ($ avalue = self: THROW_NONE) {switch ($ avalue) {case self: THROW_CUSTOM: // throw a custom exception throw new MyException ('1 is an invalid Parameter ', 5); break; case self: THROW_DEFAULT: // throw the default Exception throw new Exception ('2 isnt allowed as a parameter', 6); break; default: // create an object $ this-> var = $ avalue; break ;}}// example 1 try {$ o = new TestException (TestException :: THROW_CUSTOM);} catch (MyException $ e) {// capture exceptions echo "Caught my exception \ n", $ e; $ e-> customFunction ();} catch (Exception $ e) {// echo "Caught Default E ignored Xception \ n ", $ e;} // execute the subsequent code var_dump ($ o); echo" \ n "; // Example 2 try {$ o = new TestException (TestException: THROW_DEFAULT);} catch (MyException $ e) {// The exception type cannot be matched, ignored echo "Caught my exception \ n", $ e; $ e-> customFunction ();} catch (Exception $ e) {// capture exceptions echo "Caught Default Exception \ n", $ e;} // execute subsequent code var_dump ($ o); echo "\ n "; // Example 3 try {$ o = new TestException (TestException: THROW_CUSTOM) ;} Catch (Exception $ e) {// catch Exception echo "Default Exception caught \ n", $ e;} // execute the subsequent code var_dump ($ o ); echo "\ n"; // Example 4 try {$ o = new TestException ();} catch (Exception $ e) {// no Exception, echo "Default Exception caught \ n", $ e;} // run the subsequent code var_dump ($ o); echo "\ n";?>

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.