PHP Exception Handling

Source: Internet
Author: User
Tags php exception handling throw exception

1. What is exception handling?

Handling unhealthy states, handling errors

2. Why use exception handling?

Exception handling can help us handle errors more intelligently

3. When do I use exception handling?

Exception handling is used when connecting to the database using PDO

4. How to use exception handling?

1) Exception Handling class: Exception

Method:

GetMessage () Get exception information

GetCode () Get exception number

2) Exception handling classes can be extended to inherit

3) Capture multiple exceptions, to put exception capture in the last

/*try{attempts to perform the first business if the business has an exception, throws an exception if an exception occurs, the first business follow-up will no longer execute ... catch (Exception $e) {handling exception} second business*/Try{    Echo"The alarm clock is ringing, it's time to get up ; //$f =true;    $f=false; if(!$f){        //throws an exception        Throw New Exception("Not Up", 1); }        //If an exception occurs, the code will no longer execute    Echo"Brush your teeth, wash your face ...; }Catch(Exception $e){    //Handling Exceptions    Echo"Exception information:".$e->getmessage (). " ; Echo"Exception number:".$e->getcode (). " ; Echo"Throw exception file path:".$e->getfile (). " ; Echo"The row where the exception was thrown:".$e->getline (). " ;}Echo"Go to Work";
//Extending the default exception class exceptionclassMyExceptionextends Exception{     Public function__tostring () {return Serialize($this); }     Public functionWC () {Echo"A ; }}Try{    Echo"The alarm clock is ringing, it's time to get up ; $f 1=true; //$f 1=false;     if(!$f 1){        //throws an exception        Throw NewMyException ("No time to squat in the toilet", 0); }    Echo $e=Newmyexception; Echo"; Echo $e-WC (); Echo"Brush your teeth, wash your face ...; //$f 2=true;    $f 2=false; if(!$f 2){        //throws an exception        Throw New Exception("Not Up", 1); }    Echo"Breakfast ;}Catch(myexception$e){    //Handling Exceptions    Echo"Exception information:".$e->getmessage (). " ; Echo"Exception number:".$e->getcode (). " ; Echo"Throw exception file path:".$e->getfile (). " ; Echo"The row where the exception was thrown:".$e->getline (). " ;}Catch(Exception $e){    //Handling Exceptions    Echo"Exception information:".$e->getmessage (). " ; Echo"Exception number:".$e->getcode (). " ; Echo"Throw exception file path:".$e->getfile (). " ; Echo"The row where the exception was thrown:".$e->getline (). " ;}Echo"Go to Work";

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.