php exception and error handling mechanism
In actual development, it is far from enough to rely solely on try{}catch() to capture errors and exceptions. So quote the following functions.
Let’s talk about exceptions first:
First of all, we must understand that exceptions and errors are different. Exceptions are situations outside of normal logic, while errors refer to errors during runtime, such as using an undefined variable, etc. , exceptions need to be thrown to be caught, and errors will cause program execution to terminate
1. The usual way to handle exceptions is to use try{}catch{} to catch exceptions thrown by throw
[ php] view
plaincopy
- try{
- ); } catch(Exception $e ){
-
echo- $e->getMessage(),
""- ,$e ->getTraceAsString(); } 2. Set the exception handling function through the set_exception_handler function. In this case, even without try{}catch{}, the exception thrown by throw can be automatically set by the function set_exception_handler Capture
- [php] view
plaincopy
set_exception_handler('exceptionHandler');
- throw "kkkkkkkkkkkkkkkk");
- function exceptionHandler(Exception $exception){ getMessage();
-
} Let’s discuss errors next: Usually when a program goes wrong, PHP will output error information to help debug, but the output of this information can be controlled through the function error_reporting(). Errors in php are divided into levels and types. The following is a description of all error types:
E_ALL - all errors and warnings (excluding E_STRICT) - E_ERROR - fatal runtime errors E_WARNING - runtime warnings (non-fatal) E_PARSE - compile-time parsing errors
E_NOTICE - runtime reminders (these are often caused by bugs in your code, or may be caused by intentional behavior.) - E_STRICT - encoding standardization warnings, allowing PHP to suggest how to modify code to ensure optimal interoperability and forward compatibility. E_CORE_ERROR - Fatal error during PHP startup initialization process E_CORE_WARNING - Warning during PHP startup initialization process (non-fatal error) E_COMPILE_ERROR - Fatal error at compile time E_COMPILE_WARNING - Compile time warning (non-fatal error) E_USER_ERROR - User-defined error messageE_USER_WARNING - User-defined warning messageE_USER_NOTICE - User-defined reminder messageIf error_reporting(E_NOTICE) is set, then the program will only output E_NOTICE level information, generally we use You only need to set error_reporting(E_ALL&!E_WARNING)
- Above we see an error called User-defined error message, what is this? Let’s look at an example first
[php] view
plaincopy
- set_error_handler('errorHandler');
- trigger_error("aaaaaaassssssssssss",E_USER_ERROR);
- function errorHandler($errno, $errstr){
-
"white-space:pre"> if($errno ==E_USER_ERROR ){
-
"white-space:pre"> $errstr; "white-space:pre"> }
- "white-space:pre">
- } Output result:
- [php] view
plaincopy
innnnnnnni:aaaaaaasssssssssssss
trigger_error() is a function used to throw user-defined error messages. Through this we can throw some customized messages to be treated as errors, such as Serious logic problem -
In the above program, we see that when the program errors, in addition to letting Php output the error message by default, we can also set our own error handling function. The setting method is set_error_handler(), let’s follow Take an example
[php] view
plaincopy
set_error_handler('errorHandler'
);
- echo ;
-
echo $ cc;//$cc is not defined, echo will error
- function errorHandler($errno,$errstr
- ){ if($errno==E_NOTICE){
- ; }
- } Output result: ddddddddddddinnnnnnnni:Undefined variable: cc
The above introduces the PHP exception and error handling mechanism, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.
-