Using the Try{thow}catch () {} exception handling structure, you can effectively control the code for multiple rows of possible exceptions, the basic model is as follows:
<?PHPTry{throwNewException ("error Message";)//throws an error} Catch(Exception $e)//catch error message, exception is a built-in error-handling class{echo $e->getmessage ();//Output error messageecho $e->getcode ();//return Exception Codeecho $e->getfile ();//returns the file where the exception occurredecho $e->getline ();//returns the line number where the exception occurred}?>
Exception prototype:
<?PHPclassexception{protected$message ='Unknown Exception';//Exception Information Private$string;//__tostring Cache protected$code =0;//user-defined exception codes protected$file;//the file name of the exception that occurred protected$line;//the line number of the code where the exception occurred Private$trace;//BackTrace Private$previous;//previous exception if nested exception Publicfunction __construct ($message =NULL, $code =0, Exception $previous =NULL); FinalPrivatefunction __clone ();//inhibits cloning of exceptions.Final Publicfunction GetMessage ();//Return exception InformationFinal Publicfunction GetCode ();//return Exception CodeFinal Publicfunction GetFile ();//returns the file name of the exception that occurredFinal Publicfunction GetLine ();//returns the line number of the code where the exception occurredFinal Publicfunction Gettrace ();//backtrace () arrayFinal Publicfunction getprevious ();//before the exceptionFinal Publicfunction gettraceasstring ();//Gettrace () information that has been rasterized into a string Publicfunction __tostring ();//A string that can be output}?>
At the same time, a try can also correlate multiple catch blocks, and you can customize the class to inherit the exception class to implement
<?PHPclassMyException1 extends exception{}//Customize as needed classMyException2 extends exception{}classMyException3 extends exception{}Try{Switch(1) { Case 1:Throw NewMyException1 (); Case 2:Throw NewMyException2 (); Case 3:Throw NewMyException3 ();//When the corresponding catch code block is not found, it is thrown into exception processing because MyException3 inherits the exception}}Catch(MyException1 $e) {echo"Custom Error 1"; }Catch(MyException2 $e) {echo"Custom error 2"; }Catch(Exception $e) {echo"Primitive Exception Class"; }?>
PHP Exception Handling