Home > Backend Development > PHP Tutorial > PHP exception and error handling mechanism

PHP exception and error handling mechanism

WBOY
Release: 2016-08-08 09:19:30
Original
839 people have browsed it

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

  1. try{
  2. ); } catch(Exception $e ){
  3. echo
  4. $e->getMessage(),
  5. "
    "
  6. ,$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
  7. [php] view plaincopy

set_exception_handler('exceptionHandler'

);

  1. throw "kkkkkkkkkkkkkkkk");
  2. function exceptionHandler(Exception $exception){ getMessage();
  3. } 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:
  4. E_ALL - all errors and warnings (excluding E_STRICT)
  5. E_ERROR - fatal runtime errors E_WARNING - runtime warnings (non-fatal) E_PARSE - compile-time parsing errors
  6. E_NOTICE - runtime reminders (these are often caused by bugs in your code, or may be caused by intentional behavior.)
  7. 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)
  8. Above we see an error called User-defined error message, what is this? Let’s look at an example first

[php] view plaincopy

  1. set_error_handler('errorHandler');
  2. trigger_error("aaaaaaassssssssssss",E_USER_ERROR);
  3. function errorHandler($errno, $errstr){
  4. "white-space:pre"> if($errno ==E_USER_ERROR ){
  5. "white-space:pre"> $errstr; "white-space:pre"> }
  6. "white-space:pre">
  7. } Output result:
  8. [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
  1. 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'

);
  1. echo ;
  2. echo $ cc;//$cc is not defined, echo will error
  3. function errorHandler($errno,$errstr
  4. ){ if($errno==E_NOTICE){
  5.                                                                                                                                             ; }
  6. } 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.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template