PHP exception, error handling mechanism
In the actual development, the error and the abnormal catch only rely on Try{}catch () is not enough. Therefore, the following functions are referenced.
Let's start with the exception:
The first thing to understand is that the exception is not the same as the error, the exception is the normal logic outside the situation, and the error refers to the runtime error, for example, using an undefined variable, such as the exception needs to throw (throw) to be caught, and the error will cause the program to terminate the execution
1. The usual way to handle exceptions is to use try{}catch{} to catch exceptions thrown with throw
[PHP] View plaincopy
- try{
- Throw New Exception ("KKKKKKKKKKKKKKKK");
- }
- catch (Exception $e) {
- Echo $e->getmessage (),"
",$e->gettraceasstring ();
- }
2, set the exception handling function through the Set_exception_handler function, in this case, even if no try{}catch{},throw thrown exception can be automatically captured by the function set by Set_exception_handler
[PHP] View plaincopy
- Set_exception_handler (' Exceptionhandler ');
- Throw New Exception ("KKKKKKKKKKKKKKKK");
- function Exceptionhandler (Exception $exception) {
- Echo $exception->getmessage ();
- }
Next we discuss the error:
Usually the program goes wrong and PHP outputs the error message to help debug, but the output of this information can be controlled by the function error_reporting (). Errors in PHP are graded and kind, and the following are descriptions of all the types of errors:
E_all-All errors and warnings (not including e_strict)
E_error-Fatal Run-time error
E_warning-run-time warning (non-fatal error)
E_parse-Compile-time parse error
E_notice-Runtime Reminders (these are often caused by bugs in your code, or by intentional behavior.) )
E_strict-coding normalization warning, which allows PHP to suggest how to modify the code to ensure optimal interoperability forward compatibility.
E_core_error-php fatal error during initialization during startup
E_core_warning-php warnings during initialization during startup (non-fatal error)
E_compile_error-Compile-time fatal error
E_compile_warning-Compile-time warning (non-fatal error)
E_user_error-user-defined error message
e_user_warning-user-defined warning message
E_user_notice-User-definable reminder message
If you set error_reporting (E_notice), then the program will only output e_notice level of information, generally we use only need to set the error_reporting (e_all&! e_warning) on the line.
Above we see a bug 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" > Echo "Innnnnnnni:",$errstr;
- "White-space:pre" > }
- "White-space:pre" >
- }
Output Result:
[PHP] View plaincopy
- Innnnnnnni:aaaaaaassssssssssss
Trigger_error () is a function used to throw a user-defined error message, and some of the messages that we can throw custom are treated as errors, such as serious logic problems
The above program we see, when the program error, in addition to let PHP default output error message, we can also set their own error handling function, set the method is Set_error_handler (), see an example
[PHP] View plaincopy
- Set_error_handler (' ErrorHandler ');
- Echo "dddddddddddd
";
- Echo $cc; //$CC not defined, Echo will go wrong
- function ErrorHandler ($errno,$errstr) {
- if ($errno==e_notice) {
- Echo "Innnnnnnni:",$errstr;
- }
- }
Output Result:
Dddddddddddd
innnnnnnni:undefined VARIABLE:CC
The above describes the PHP exception, error handling mechanism, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.