PHP error Handling exception handling control function

Source: Internet
Author: User
Tags php error try catch vars

In the actual development, the error and the abnormal catch only rely on Try{}catch () is not enough.

  try {      //在这里主动抛出异常,(在这里说一下,php正常开发中最好不要主动抛出异常)   throw new Exception( ‘Nicht aufgefangene Exception‘ );   }   catch (Exception  $e )   {     var_dump( $e );   }


Therefore, the following functions are referenced.

Error_reporting (e_all); Set exception error display level 0 is forbidden error

//禁用错误报告 error_reporting (0); //报告运行时错误 error_reporting (E_ERROR | E_WARNING | E_PARSE); //报告所有错误 error_reporting (E_ALL); //除去提醒处理 error_reporting (E_ALL~E_NOTICE);

Set_exception_handler when an exception try catch is not captured, a parameter is executed as a custom error handler, can be the first value of the array is that class, and the second value is the method inside the class

Register_shutdown_function a function that executes when a PHP script execution is about to close, a script is executed, or an error occurs. If you set this function again before, it will trigger the argument. A function name is used for processing, can be built into the system, or it can be customized.

Trigger_error user to customize an exception error first parameter error content Second optional parameter error level

error_get_last () gets the last exception error
a)    set_error_handler  Set exception handler A parameter is a custom error handler that executes, can be an array where the first value is that class, and the second value is what the method
is used to capture   E_notice, E_user_error, e_user_warning, E_ User_notice
cannot be captured:
E_error, E_parse, E_core_error, e_core_warning, E_compile_error and e_compile_warning. The
is generally used in conjunction with Trigger_error ("...", e_user_error).

[PHP] View plaincopyprint?      <?php//We'll do our own error handling error_reporting (0);              function Usererrorhandler ($errno, $errmsg, $filename, $linenum, $vars) {//timestamp for the error entry              $DT = Date ("y-m-d h:i:s (T)"); Define an assoc array of error string//In reality the only entries we should//consider AR                              E e_warning, E_notice, E_user_error,//e_user_warning and E_user_notice $errortype = Array ( E_error = ' ERROR ', e_warning = ' Wa             Rning ', E_parse = ' parsing Error ', E_notice                              = ' Notice ', e_core_error = ' CORE ERROR ', e_core_warning = ' CORE WARNING ', E_compile_error = 'Compile Error ', e_compile_warning = ' Compile WARNING ', e_                              User_error = ' user ERROR ', e_user_warning = ' User WARNING ', E_user_notice = ' USER NOTICE ', e_strict = ' Runti              Me Notice ', e_recoverable_error = ' catchable Fatal ERROR '); Set of errors for which a var trace would be saved $user _errors = Array (e_user_error, E_user_wa                  Rning, E_user_notice);              $err = "<errorentry>\n"; $err. = "\t<datetime>". $dt.              "</datetime>\n"; $err. = "\t<errornum>". $errno.              "</errornum>\n"; $err. = "\t<errortype>". $errortype [$errno].              "</errortype>\n"; $err. = "\t<errormsg>". $errmsg.     "</errormsg>\n";         $err. = "\t<scriptname>". $filename.              "</scriptname>\n"; $err. = "\t<scriptlinenum>". $linenum.              "</scriptlinenum>\n"; if (In_array ($errno, $user _errors)) {$err. = "\t<vartrace>". Wddx_serialize_value ($vars, "Var Iables ").              "</vartrace>\n";          } $err. = "</errorentry>\n\n";      Echo $err;                      } function Distance ($vect 1, $vect 2) {if (!is_array ($vect 1) | |!is_array ($vect 2)) {                      Trigger_error ("Incorrect parameters, arrays expected", e_user_error);              return NULL; } if (count ($vect 1)! = Count ($vect 2)) {Trigger_error ("Vectors need to be of the same SI                      Ze ", e_user_error);              return NULL;                      } for ($i =0; $i <count ($vect 1); $i + +) {$c 1 = $vect 1[$i]; $c 2 = $vect 2[$i];        $d = 0.0;              if (!is_numeric ($c 1)) {trigger_error ("coordinate $i in Vector 1 are not a number, u                          Sing Zero ", e_user_warning);                  $c 1 = 0.0; } if (!is_numeric ($c 2)) {trigger_error ("coordinate $i in Vector 2 are not a numb                          Er, using zero ", e_user_warning);                  $c 2 = 0.0;              } $d + = $c $c 2-$c 1* $c 1;      } return sqrt ($d);      } $old _error_handle = Set_error_handler ("Usererrorhandler");  $t = i_am_not_defined;      Generates a warning//define some "vectors" $a = Array (2, 3, "foo");      $b = Array (5.5, 4.3,-1.6);            $c = Array (1,-3);            Generate a user error $t 1 = distance ($c, $b); Generate another user error $t 2 = distance ($b, "I am not an array").            "\ n"; Generate a warning $t 3 = distance ($a, $b).      "\ n";   ?>

 b)    set_exception_handler
Sets the default exception handler for exceptions that are not caught with the try/catch block. The exception is aborted after the Exception_handler call. The
is associated with the throw new Exception (' uncaught Exception occurred ').

    <?php//We'll do our own error handling error_reporting (0);              function Excepthandle ($errno, $errmsg, $filename, $linenum, $vars) {//timestamp for the error entry              $DT = Date ("y-m-d h:i:s (T)"); Define an assoc array of error string//In reality the only entries we should//consider AR                              E e_warning, E_notice, E_user_error,//e_user_warning and E_user_notice $errortype = Array ( E_error = ' ERROR ', e_warning = ' Wa             Rning ', E_parse = ' parsing Error ', E_notice                              = ' Notice ', e_core_error = ' CORE ERROR ',               e_core_warning = ' CORE WARNING ', E_compile_error = ' COMPILE ERROR ',               e_compile_warning = ' COMPILE WARNING ', e_user_error = ' USER Error ', e_user_warning = ' USER WARNING ', e_user_notic                              E = ' User Notice ', e_strict = ' Runtime Notice ',              E_recoverable_error = ' catchable Fatal ERROR ');              Set of errors for which a var trace would be saved $err = "<errorentry>\n"; $err. = "\t<datetime>". $dt.              "</datetime>\n"; $err. = "\t<errornum>". $errno.              "</errornum>\n"; $err. = "\t<errortype>". $errortype [$errno].              "</errortype>\n"; $err. = "\t<errormsg>". $errmsg.              "</errormsg>\n"; $err. = "\t<scriptname>". $filename.              "</scriptname>\n"; $err. = "\T&LT;SCRIPTLInenum> ". $linenum.              "</scriptlinenum>\n"; if (1) {$err. = "\t<vartrace>". Wddx_serialize_value ($vars, "Variables").              "</vartrace>\n";          } $err. = "</errorentry>\n\n";      Echo $err;      } $old _except_handle = Set_exception_handler ("Excepthandle");    $t = i_am_not_defined;      Generates a warning $a;          throw new Exception (' uncaught Exception occurred ');   ?>

c) register_shutdown_function
The execution mechanism is: PHP calls the function to call into memory. Call this function when all the PHP statements on the page are completed.
Generally with trigger_error ("...", E_user_error), with the use.

    <?php      error_reporting (0);      Date_default_timezone_set (' Asia/shanghai ');      Register_shutdown_function (' My_exception_handler ');            $t = i_am_not_defined;  Generates a warning      trigger_error ("Vectors need to be of the same size", e_user_error);                 function My_exception_handler ()      {          if ($e = Error_get_last ()) {          //$e [' type '] corresponds to PHP_ERROR constant          $message = ";          $message. = "error message: \ t". $e [' message ']. " \ n ";          $message. = "Error file: \ t". $e [' file ']. " \ n ";          $message. = "Number of error lines: \ t". $e [' line ']. " \ n ";          $message. = "\t\t please engineer to check for program". $e [' file ']. " The cause of the error \ n ";          $message. = "\t\t hope you can resolve the cause of the failure early <br/>";          echo $message;          SendEmail to          }      }      ?>  

c) Restore_error_handler () function

Define and use the restore_error_handler() function to restore the previous error handler, which was changed by the Set_error_handler () function.

The function always returns TRUE.

is the inverse function of the set_error_handler ().

Each time you call the function the next occurrence of an exception will execute the previous definition of the wrong function, if you have been restored until the recovery until the previous processing function will be an error;

mysql_connect ("inexistent");//generate an error.    The actual error handler is set by default function Foo1 () {echo "<br>error foo1<br>";}    function Foo2 () {echo "<br>error foo2<br>";}        function Foo3 () {echo "<br>error foo3<br>";}    Set_error_handler ("foo1");    Current error Handler:foo1 Set_error_handler ("Foo2");    Current error Handler:foo2 Set_error_handler ("Foo3");        Current error Handler:foo3 mysql_connect ("inexistent");        Restore_error_handler ();         Now, current error Handler:foo2 mysql_connect ("inexistent");        Restore_error_handler ();     Now, current error handler:foo1 mysql_connect ("inexistent");        Restore_error_handler ();    Now the current error Handler:default handler mysql_connect ("Inexistent");        Restore_error_handler (); Now current error Handler:default handler (the stack Can ' t pops more) 

PHP error Handling exception handling control function

Related Article

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.