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