PHP Exception Handling

Source: Internet
Author: User
Tags php exception handling

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

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.