Error handling of PHP

Source: Internet
Author: User
Tags php error throw exception

Error Reporting

Error occurrences of PHP programs are generally attributed to the following three areas:

1. Syntax error

Syntax errors are most common and easy to fix. such as: A semicolon is omitted from the code. Such errors can prevent the execution of the script

2. Run-time error

This error generally does not prevent the PHP script from executing, but it blocks what is currently being done. Output an error, but the PHP script continues to execute

3. Logic Error

This error is the most troublesome and does not prevent the script from executing or outputting error messages

[note] If the display_errors in the php.ini configuration file is set to off from the default on, no error will be displayed

The Ini_set () function can be called in a PHP script to dynamically set the php.ini configuration file

Ini_set ("Display_errors", "on"); Show all error messages

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/740839/201611/740839-20161122110157409-1884952667. JPG "style=" border:none;margin-top:20px;margin-bottom:20px; "/>

Error level

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/740839/201611/740839-20161122095037846-104492133. JPG "style=" border:none;margin-top:20px;margin-bottom:20px; "/>

In fact, the 13 error types in a table can be divided into 3 categories: note level, warning level, and error level. Generally, in the development process, ignore the error of the attention level

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

<?php GetType ($a);//undefined variable, note level echo "1111111111111111<br>";    GetType ();//not passed in parameter, warning level echo "222222222222222222222<br>"; GetType3 ();//Function name error, error level echo "333333333333333333333<br>";? >

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/740839/201611/740839-20161122095210050-904822047. JPG "style=" border:none;margin-top:20px;margin-bottom:20px; "/>

Error handling

1. The first method of error handling is to modify the configuration file

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/740839/201611/740839-20161122100603456-980490190. JPG "style=" border:none;margin-top:20px;margin-bottom:20px; "/>

Error level defaults to prompt for all levels of error: Error_reporting = E_all

Change error_reporting = E_all to error_reporting = E_all & ~e_notice for errors that do not prompt for attention levels. Then, restart the service to take effect

error_reporting = E_all & ~e_notice throws any non-noticeable error, default value error_reporting = E_error | E_parse | E_core_error only considers fatal run-time errors, new parsing errors, and core errors error_reporting = E_all & ~ (E_user_error | e_user_warning | E_user_notice) reports all errors except user-caused errors

2. The second method of error handling is to use the error handling function

In PHP scripts you can dynamically set the error reporting level via the error_reporting () function

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

<?php error_reporting (E_all & ~e_notice);    GetType ($a);//attention level echo "1111111111111111<br>";    GetType ();//Warning Level echo "222222222222222222222<br>"; GetType3 ();//Error level echo "333333333333333333333<br>";? >

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/740839/201611/740839-20161122101242362-598241698. JPG "style=" border:none;margin-top:20px;margin-bottom:20px; "/>

Custom error Handling

Custom error report processing, you can completely bypass the standard PHP error handler, so that you can print the error report in the format you define, or change the location of the error report printing, the following situations can be considered custom error handling: 1, write down the wrong information, timely detection of some production environment problems; 2 , shielding error, 3, the output of control error; 4. As a debugging tool

Use the Set_error_handler () function to set user custom error handling

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

<?php    //error_reporting (e_all & ~e_notice);     // Register a function in PHP to handle error reporting, instead of the default way     set_error_handler ("Myerrorfun");     $ mess =  "";     //custom Error Reporting handler function     function myerrorfun ($ error_type,  $error _message,  $error _file,  $error _line)  {         global  $mess;         $mess. = "Error level has occurred {$error _ Type},  error message <b>{$error _message}</b>,  in file <font style= ' color:red ' >{$error _file }</font> in,  {$error _line} line. <br> ";         }    gettype ($a);     echo  "1111111111111111<br>";     gettype ();     echo  "222222222222222222222<br>";    echo  "--------------------------------------------<br> ";    echo  $mess;? >

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/740839/201611/740839-20161122105537518-795497495. JPG "style=" border:none;margin-top:20px;margin-bottom:20px; "/>

Error log

In general, the program saves the error log, which is used to log error messages when the program is running. And the error log has its default storage location. We can modify the error message and the location of the error log.

In the php.ini configuration file, the following items can be set for the error log

error_reporting = e_all//will send each error to PHP Display_errors=off//Do not display error report Log_errors=on//Determine the location of log statement records log_errors_m ax_log=1024//Maximum length per log entry Error_log=g:/myerror.log//Specify error-written file

In the php file, we can use the function error_log () to customize the error message

<?phperror_log ("Login failed! ");? >

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/740839/201611/740839-20161122112328893-253913128. JPG "style=" border:none;margin-top:20px;margin-bottom:20px; "/>

Exception handling

Exception (Exception) processing is used to alter the normal flow of scripts when a specified error occurs, and is a new and important feature in PHP5. Exception handling is an extensible and maintainable error handling mechanism, and provides a new object-oriented error handling method.

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

Try{uses a try to include code that may occur an exception if an exception is caught in the try to catch the exception, given to catch processing. Throw Exception Statement: Throw exception object. }catch (Exception object parameter) {Do exception handling here.} }[catch (. ,,){ .. .. ..}]

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

Custom exceptions

Users can use custom exception handling classes to extend PHP's built-in exception handling classes. The following code shows which properties and methods are accessible and inheritable in a subclass of the built-in exception-handling class

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

<?phpclass Exception{    protected  $message  =  ' unknown  Exception ';   //  exception info     private    $string;                            // __toString cache    protected  $code  = 0;                         //  user Custom Exception codes     protected  $file;                             //  filename of exception occurred      protected  $line;                             //  the code line number of the exception that occurred     private     $trace;                            // backtrace     private    $previous;                         // previous exception  If nested exception    public function __construct ($message  =  null,  $code  = 0, Exception  $previous  = null);     Final private function __clone ();            // Inhibits cloning of exceptions.    final public   function getmessagE ();        //  return exception information     final public   function getcode ();           //  Returns the exception code     final public  function getfile ();            //  returns the file name of the exception that occurred     final public   function getline ();           //  Returns the line number of the code where the exception occurred     final public  function gettrace ();           // backtrace ()   array     final public   function getprevious ();       //  before  exception     final public  function gettraceasstring ();  //   gettrace (), which has been converted into a string ()   Information &Nbsp;   // overrideable    public function __tostring ();                //  a string to output}? >

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

[note] If you use a custom class to extend the built-in exception-handling class, and you want to redefine the constructor, it is recommended to call Parent::__construct () at the same time to check that all variables have been assigned values. You can overload __tostring () and customize the style of the output when the object is going to output a string

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

<?php    /*  a custom exception-handling class, but must be a subclass of the exception-handling class within the extension  */    class  myexception extends exception{        //redefine the constructor to make the first parameter   message  becomes a property that must be specified         public function __construct ($ message,  $code =0) {            // You can define some of your own code here          //suggest calling  parent::construct () at the same time To check that all variables have been assigned a value of             parent::__construct ($ message,  $code);        }             public function __tostring ()  {                   //overriding the parent class method, customizing the style of the string output            return __class__. ": [". $this->code. "]:". $this->message. " <br> ";        }         Public function customfunction ()  {                  //customizing a processing method for this exception               echo  "Handle this type of exception that occurs by custom method <br>";         }    }?>

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>

<?php   try { //catches an exception with a custom exception class and handles exceptions           $error  =  ' Allow this error to be thrown ';                throw new myexception ($error);                  //Create a custom Exception class object, thrown by the throw statement          echo  ' never executed ';              //from here, the code inside the try code block will no longer be executed     } catch  (myexception $ e)  {        //Capturing custom exception objects          echo  ' catch Exception:  '. $e;         //output caught exception message           $e->customfunction ();   //handling exceptions through the methods in the custom exception object      }    echo  ' Hello ';               // Program does not crash continue down execution?>


Error handling of PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.