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";?>