Frontend PHP error handling, frontend php Error

Source: Internet
Author: User
Tags php error

Frontend PHP error handling, frontend php Error
* Directory [1] Error Report [2] error level [3] error handling [4] custom error [5] Error Log [6] exception handling [7] Before custom exception words

Error handling is critical to program development. errors that may occur cannot be predicted in advance, and recovery policies cannot be taken in advance, which may lead to poor user experience. This article describes in detail how to handle PHP errors.

[Note] the error handling mechanism of javascript is now

 

Error Report

PHP program errors generally occur in the following three fields:

1. Syntax Error

Syntax errors are the most common and easy to fix. For example, a semicolon is missing in the code. These errors will prevent script execution.

2. runtime error

This type of error generally does not stop PHP script execution, but does prevent the current task. An error is output, but the php script continues to be executed.

3. logical error

This type of error is the most troublesome. It neither prevents script execution nor outputs error messages.

[Note] If you set display_errors in the php. ini configuration file from the default on to off, no errors will be displayed.

In the PHP script, you can call the ini_set () function to dynamically set the php. ini configuration file.

Ini_set ("display_errors", "On"); // display all error messages

 

Error level

In fact, the 13 error types in the table can be divided into three types: Attention Level, warning level, and error level. Generally, errors at the attention level are ignored during development.

<? Php getType ($ a); // undefined variable. Note the echo "1111111111111111 <br>"; getType (); // No parameter is input, warning Level echo "222222222222222222222 <br>"; getType3 (); // function name error, error level echo "333333333333333333333 <br>";?>
Error Handling

1. The first method to handle errors is to modify the configuration file.

By default, error levels indicate errors of all levels: error_reporting = E_ALL

Change error_reporting = E_ALL to error_reporting = E_ALL &~ E_NOTICE indicates that no attention-level error is prompted. Then, restart the service to take effect.

Error_reporting = E_ALL &~ E_NOTICE throws any non-Attention errors. The default value is error_reporting = E_ERROR | E_PARSE | E_CORE_ERROR. Only fatal runtime errors, new parsing errors, and core errors are considered. error_reporting = E_ALL &~ (E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE) reports all errors except user-caused errors

2. The second method is to use the error processing function.

In the PHP script, you can dynamically set the error report level through the error_reporting () function.

<? Php error_reporting (E_ALL &~ E_NOTICE); getType ($ a); // note level echo "1111111111111111 <br>"; getType (); // warning level echo "222222222222222222222 <br> "; getType3 (); // error level echo "333333333333333333333 <br>";?>
Custom error handling

The custom error report processing method can completely bypass the standard PHP error handling function, so that you can print the error report in your own defined format or change the location where the error report is printed, custom error handling can be considered in the following situations: 1. Write down the error information and promptly discover problems in the production environment; 2. shield the error; 3. Control the output of the error; 4. As a debugging tool

Use the set_error_handler () function to set custom error handling.

<? Php // error_reporting (E_ALL &~ E_NOTICE); // register a function in php to handle the error report. The default method is set_error_handler ("myerrorfun"); $ mess = ""; // function myerrorfun ($ error_type, $ error_message, $ error_file, $ error_line) {global $ mess; $ mess. = "error level: {$ error_type} type, error message <B >{$ error_message} </B>, in the file <font style = 'color: in red '> {$ error_file} </font>, row {$ error_line. <Br> ";} getType ($ a); echo" 1111111111111111 <br> "; getType (); echo" 222222222222222222222 <br> "; echo" ------------------------------------------ <br> "; echo $ mess;?>
Error Log

Generally, the program stores error logs, which are used to record the error information when the program is running. And error logs all have their default storage locations. You can modify the error information and the location of the error log.

The following items can be set in the PHP. ini configuration file:

Error_reporting = E_ALL // each error display_errors = Off will be sent to PHP // No Error Report will be displayed log_errors = On // determine the location recorded by the log statement log_errors_max_log = 1024 // For each log item maximum length: error_log = G: /myerror. log // specify the file to which the error is written

In the PHP file, we can use the error_log () function to customize error messages.

<? Phperror_log ("Logon Failed! ");?>

 

Exception Handling

Exception Processing is a new important feature in PHP5 to change the normal process of the script when a specified error occurs. Exception Handling is a scalable and easy-to-maintain error handling mechanism, and provides a new object-oriented error handling method.

Try {use try to include code that may cause exceptions. Once an exception occurs, try to catch the exception and submit it to catch for handling. Throw exception statement: throw exception object .} Catch (exception object parameter) {handle exceptions here .} [Catch (.,,) {...}]
<? Php try {$ error = 'always throw this error'; throw new Exception ($ error); // create an Exception object and throw echo 'never executed' through the throw statement '; // from here, the code in the try code block will not be executed again} catch (Exception $ e) {echo 'caught exception :'. $ e-> getMessage (). "\ n"; // output the captured exception message} echo 'Hello world'; // the program does not crash and continues to run down?>

 

Custom exception

You can use a custom exception handling class to extend the PHP built-in exception handling class. The following code illustrates which attributes and methods are accessible and inherited in the built-in exception handling class.

<? Phpclass Exception {protected $ message = 'unknown exception'; // exception information private $ string; // _ toString cache protected $ code = 0; // User-Defined Exception Code protected $ file; // the file name protected $ line when an exception occurs; // The code line with the exception is private $ trace; // backtrace private $ previous; // previous exception if nested exception public function _ construct ($ message = null, $ code = 0, Exception $ previous = null); final private function _ c Lone (); // Inhibits cloning of exceptions. final public function getMessage (); // returns the exception message final public function getCode (); // returns the Exception Code final public function getFile (); // return the file name final public function getLine () for which an exception occurs; // return the code line number final public function getTrace (); // backtrace () array final public function getPrevious (); // The previous exception final public function getTraceAsString (); // getTrace () Message formatted as a string Information // Overrideable public function _ toString (); // output string}?>

[Note] If you use a custom class to extend the built-in exception handling class and want to redefine the constructor, we recommend that you call parent :__ construct () at the same time () to check whether all variables have been assigned values. When an object needs to output a string, you can reload _ toString () and customize the output style.

<? Php/* a custom exception handling class, however, it must be a subclass of the extended Exception handling class */class MyException extends Exception {// redefine the constructor so that the first parameter message is changed to the public function _ construct ($ message, $ code = 0) {// you can define your own code here. // we recommend that you call parent: construct () to check whether all variables have been assigned parent :: __construct ($ message, $ code);} public function _ toString () {// rewrite the parent CLASS method and customize the Style return _ CLASS _ output of the string __. ":[". $ this-> code. "]:". $ this-> message. "<br>";} public function customFuncti On () {// customize a processing method for this exception echo "handle this type of exception by custom method <br>" ;}}?>
<? Php try {// capture an exception using a custom exception class and handle the exception $ error = 'Allow throwing this error'; throw new MyException ($ error ); // create a custom exception class object and throw echo 'never executed' through the throw statement; // from here, the code in the try code block will not be executed again} catch (MyException $ e) {// capture the custom exception object echo 'catch exception :'. $ e; // output the captured exception message $ e-> customFunction (); // process the exception through the methods in the custom exception object} echo 'Hi '; // The program does not crash and continues to run down?>

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.