Exception handling of PHP object-oriented programming

Source: Internet
Author: User

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

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.