This article mainly introduces PHP Exception Handling analysis. This article focuses on how to capture exceptions and provides code operation examples. For more information, see the following two Exception classes in PHP: Exception and ErrorException.
The Code is as follows:
Exception {
/* Attribute */
Protected string $ message; // exception message content
Protected int $ code; // Exception code number
Protected string $ file; // file name that throws an exception
Protected int $ line; // The row number in the file that throws an exception
/* Method */
Public _ construct ([string $ message = "" [, int $ code = 0 [, Exception $ previous = null])
Final public string getMessage (void) // information thrown by an exception
Final public Exception getPrevious (void) // previous Exception
Final public int getCode (void) // Exception Code, which is user-defined
Final public string getFile (void) // file path strength in case of an exception
Final public int getLine (void) // the row with an exception
Final public array getTrace (void) // exception tracking information (array)
Final public string getTraceAsString (void) // exception tracking information (string)
Public string _ toString (void) // the return value of the subfunction called when an exception object is used as a string.
Final private void _ clone (void) // called when an exception object is cloned
}
The Code is as follows:
ErrorException extends Exception {
/* Attribute */
Protected int $ severity;
/* Method */
Public _ construct ([string $ message = "" [, int $ code = 0 [, int $ severity = 1 [, string $ filename = _ FILE __[, int $ lineno = _ LINE _ [, Exception $ previous = NULL])
Final public int getSeverity (void)
/* Inherited method */
Final public string Exception: getMessage (void)
Final public Exception: getPrevious (void)
Final public int Exception: getCode (void)
Final public string Exception: getFile (void)
Final public int Exception: getLine (void)
Final public array Exception: getTrace (void)
Final public string Exception: getTraceAsString (void)
Public string Exception :__ toString (void)
Final private void Exception :__ clone (void)
}
So how to capture exceptions?
(1) PHP can catch exceptions using try... catch.... The code for exception handling must be within the try code block.
The Code is as follows:
Try {
Throw new Exception ('exception test 1', 1001 );
} Catch (Exception $ e ){
Echo $ e-> getMessage (). '-'. $ e-> getCode ();
}
(2) You can customize the exception handling function [set_exception_handler], which is not used to catch exceptions by try/catch.
The Code is as follows:
Function exception_handler ($ e ){
Echo "Uncaught exception:", $ e-> getMessage (), "\ n ";
}
Set_exception_handler ('exception _ handler ');
Throw new Exception ('uncaught exception ');
Echo "this line will not be executed ";
We can see that the ser_exception_handler callback function is used to handle exceptions. Subsequent code will not be executed, but try-catch is acceptable.
(3) PHP can catch different types of exceptions with multiple catch methods, and can throw an exception again in the catch code block.
The Code is as follows:
// Expand the exception class based on the actual situation
Class MyException extends Exception {
Public function _ construct ($ message = '', $ code = 0 ){
}
Public function myFunction (){
Echo 'just for test ';
}
}
Try {
Throw new MyException ('an error ');
} Catch (MyException $ e ){
Echo $ e-> myFunction ();
} Catch (Exception $ e ){
Echo $ e-> getMessage ();
}
(4) PHP5.5 supports the finally keyword. You do not need to worry about whether the exception overflows.
The comparison is as follows:
The Code is as follows:
Function doSomething (){
$ Resource = createResource ();
Try {
$ Result = useResource ($ resource );
} Catch (Exception $ e ){
ReleaseResource ($ resource );
Log ($ e-> getMessage ());
Exit ();
}
ReleaseResource ($ resource );
Return $ result;
}
// After using finally
Function doSomething2 (){
$ Resource = createResource ();
Try {
$ Result = useResource ($ resource );
Return $ result;
} Catch (Exception $ e ){
Log ($ e-> getMessage ());
Exit ();
} Finally {
ReleaseResource ($ resource );
}
}