1. Built-in exception handling class exception<?PHPTry{$num=2; if($num = =1) {echo"Success"; }Else{ Throw NewException ();//throws an exception, exception is a class name } }Catch(Exception $e) {//Catching exceptionsEcho $eGetFile (); Echo $e-GetLine (); Echo $e-GetCode (); Echo $e-GetMessage (); }?>2. Custom exception handling classes, inheriting the built-in exception class<?PHPclassMyException extends exception{ Publicfunction Getallinfo () {return "the exception is sent with the following file: {$this-GetFile ()}, the number of rows for the exception is: {$this, GetLine ()}, exception information is: {$this, GetMessage ()}, the code for the exception is: {$ This, GetCode ()}"; }}Try{ if($_get['Num'] ==5){ Throw NewMyException ("This is a custom exception",123456);//throws an exception, automatically jumps to the catch} Echo"Success";}Catch(MyException $e) {echo $e-getallinfo ();}?>3. Catch multiple exceptions, because there are built-in exception-handling classes, or you can have your own custom exception-handling classes<?PHPclassMyException extends exception{ Publicfunction Getallinfo () {return$ This-GetMessage (); }}Try{ if($_get['Num'] ==1){ Throw NewMyException ("This is a custom exception handling class"); }elseif ($_get['Num'] ==2){ Throw NewException ("This is the system's exception handling"); } Echo"Success";}Catch(MyException $e) {echo $e-Getallinfo (); Echo"111";}Catch(Exception $e) {echo $e-GetMessage (); Echo"222";}?>
Exception handling of PHP object-oriented programming