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 catchcorresponding 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.
Example: Throws and catches an exception
<?phptry{throw new Exception ("Catch a Exception", 42);} catch (Exception $e) {echo $e;}? >
Users can use custom exception handling classes to extend PHP's built-in exception handling classes.
PHP Built-in exception class: (Translator Note: The following code is only to illustrate the structure of the built-in exception-handling class, it is not a meaningful code available.) )
<?phpclass exception{ protected $message = ' Unknown Exception '; Exception information protected $code = 0; User-defined exception code protected $file; The file name of the exception that occurred protected $line; The code line number where the exception occurred function __construct ($message = null, $code = 0); Final function getMessage (); Returns the exception information final function GetCode (); Returns the exception code final function GetFile (); Returns the file name of the exception that occurred final function getLine (); Returns the line number of the code where the exception occurred final function gettrace (); BackTrace () array final function gettraceasstring (); Gettrace () information that has been rasterized into a string */* can be overloaded with the method * /function __tostring (); A string that can be output}?>
If you use a custom class to extend the built-in exception-handling class, and you want to redefine the constructor, it is recommended that you call Parent::__construct () at the same time to check that all variables have been assigned values. You can overload __tostring () and customize the style of the output when the object is about to output a string.
Example #2 extending PHP built-in exception handling class
<?php/** * Customize an exception Handling class */class MyException extends Exception {//redefine the constructor to make the message a property that must be specified public functio n __construct ($message, $code = 0) {//Custom code//Make sure all variables are correctly assigned to the parent:: __construct ($mes Sage, $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 '; }}/** * Create a class to test 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_cust OM://Throw custom Exception throw new MyException (' 1 is an invalid parameter ', 5); Break Case self:: throw_default: Throws the default exception throw new Exception (' 2 isnt allowed as a parameter ', 6); Break Default://Without exception, create an object $this var = $avalue; Break }}}//Example 1 try {$o = new TestException (testexception:: Throw_custom);} catch (MyException $e) {// Catch exception echo "Caught my exception\n", $e; $e-Customfunction ();} catch (Exception $e) {//ignored echo "Caught Default exception\n", $e;}//execute subsequent code var_dump ($o); echo "\ N\n "; Example 2 try {$o = new TestException (testexception:: Throw_default);} catch (MyException $e) {//Cannot match exception The kind that is ignored by echo "Caught my exception\n", $e; $e-Customfunction ();} catch (Exception $e) {//catch exception 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 subsequent code var_dump ($o); echo "\ n"; Example 4 try {$o = new testexception ();} catch (Exception $e) {//no exception, ignored echo "Default Exception Caught\n ", $e;} Execute subsequent code var_dump ($o); echo "\ n";?>
PHP Errors and exception handling