PHP is scheduled for two exception classes: Exception and Errorexception
Exception { /*Properties*/ protected string $message;//Exception message Content protectedInt$code;//Exception code number protected string $file;//the file name that throws the exception protectedInt$line;//throws the line number of the exception in the file /*Method*/ Public__construct ([string $message= "" [, int$code= 0 [,Exception $previous=NULL]]] ) Final Public stringGetMessage (void)//exception-Thrown information Final Public ExceptionGetPrevious (void)//Previous Exception Final Publicint GetCode (void)//The exception code, which is user-defined Final Public stringGetFile (void)//the file that happened to the exception path strength Final Publicint getLine (void)//the row where the exception occurred Final Public ArrayGettrace (void)//Exception tracking Information (array) Final Public stringGettraceasstring (void)//Exception tracking information (string) Public string__tostring (void)//call the return value of a child function when attempting to use the exception object as a string directly Final Privatevoid __clone (void)//called when cloning an exception object}
Errorexceptionextends Exception { /*Properties*/ protectedInt$severity ; /*Method*/ Public__construct ([string $message= "" [, int$code= 0 [, int$severity= 1 [,string $filename=__file__[, int$lineno=__line__[,Exception $previous=NULL ]]]]]] ) Final Publicint getseverity (void)/*Methods of Inheritance*/ Final Public string Exception::getMessage (void)Final Public Exception Exception::getprevious (void)Final PublicIntException::getcode (void)Final Public string Exception::getFile (void)Final PublicIntException::getLine (void)Final Public Array Exception::gettrace (void)Final Public string Exception::gettraceasstring (void) Public string Exception::__tostring (void)Final PrivatevoidException::__clone (void)}
So how do I catch an exception?
(1) PHP available try...catch ... To catch an exception, the code for exception handling must be inside a try code block.
Try { thrownewException(' Exception Test 1 ', 1001catch( Exception$e) { echo$e->getmessage (). ' -‘. $e-GetCode ();}
(2) The user can customize the exception handling function [Set_exception_handler], which is used for exceptions caught with Try/catch.
function $e {echo "uncaught exception:" , $e , GetMessage (), "\ n" /c11>set_exception_handler (' Exception_handler 'thrownew Exception (' uncaught Exception 'echo "This line will not be executed";
You can see that the exception is handled using the Ser_exception_handler callback function, and subsequent code does not continue, but Try-catch can.
(3) PHP can catch different types of exceptions with multiple catch and allow the exception to be thrown again within the catch code block.
// please according to the actual extended exception class Beijing kiddy| Hebei PVC Plastic Flooring
classMyExceptionextends Exception { Public function__construct ($message= ",$code= 0) { } Public functionmyFunction () {Echo' Just for test '; }} Try { Throw NewMyException (' An error ');} Catch(myexception$e) { Echo $e-myFunction ();} Catch(Exception $e) { Echo $e-getMessage ();}
(4) PHP5.5 has supported the FINALLY keyword, you do not have to worry about whether the exception overflow.
Can be compared as follows:
functiondosomething () {$resource=Createresource (); Try { $result= Useresource ($resource); } Catch(Exception $e) {Releaseresource ($resource); Log($e-getMessage ()); Exit(); } releaseresource ($resource); return $result;} //after using finallyfunctiondoSomething2 () {$resource=Createresource (); Try { $result= Useresource ($resource); return $result; } Catch(Exception $e) { Log($e-getMessage ()); Exit(); } finally {Releaseresource ($resource); }}
php Exception handling resolves this