PHP error handling and exception handling

Source: Internet
Author: User
Tags php error

There are two types of error handling in PHP: standard error handling and exceptions (the new error handling mechanism for OOP syntax)

Standard errors are caused by: Error level, error message, error occurrence file, and error occurrence line number. PHP provides a variety of error levels to describe the importance of errors.

Standard error two trigger forms:
PHP Automatic Trigger: PHP detects an error, it will trigger the error
User script triggers:
Use built-in function trigger_error (' Error message ', ' Error level ');
The error level can only be represented by constants: The default is NOTICE level: E_user_notice
Warning: e_user_warning, fatal error: E_user_error

Handling: Error Reporting

Output error messages directly to standard output (standard error)
This function can be controlled by the configuration of PHP

Error_reporting control which level of error should be reported
Display_errors whether to turn on the page error function (on the page output, do not turn on the output)
The above two configurations are supported in user script modification: via Ini_set (' Name of configuration item ', value)

Show All errors: e_all| E_strict, Other: E_notice,e_waring,e_error
Common in development, open all: Ini_set (' display_errors ', e_all| E_STRICT);
In production, close all: Ini_set (' Display_errors ', 0)

error Log :
Logs information to log file in the event of an error
Whether to turn on error log: Ini_set (' log_errors ', 1);
Error log location: Ini_set (' error_log ', d:/error.log);
Note: Error logs and error reports can be present at the same time.

Custom Error Handler:
The user defines a function that, once an error occurs, executes the user's function to handle the function, a user-defined function called the error handler
Set a user-defined error handler: Set_error_handler ("User-defined function")


For error level handling, fatal errors cannot be handled (cannot be recovered from the error)
If the user's error handler is customized, E_user_error can be restored.

Once the user error handler is defined, will the system's error handling be performed?
No, if the user's custom processor returns false, it means that the user has finished processing and left the system to continue processing
In a project, it is usually divided into development mode and production mode:

Development mode, showing all error reports:
Ini_set (' error_reporting ', e_all| E_STRICT);
Ini_set (' display_errors ', 1);
Ini_set (' Log_errors ', 0);
Production mode, block all error reports, but log error logs
Ini_set (' Display_errors ', 0);
Ini_set (' error_log ', ' d:/error.log ');
Ini_set (' log_errors ', 1);

Error Handling exception

Exception is a kind of PHP to OOP syntax, provides a typical error processing method, in the OOP syntax, you can be in the problem, through the exception of the way to manage, exception is also a mistake, but to put the error information in an object, exception has a premise, to ensure that the syntax is correct
The handling of exceptions consists of the occurrence and processing:
The so-called occurrence: refers to throwing an exception, using the throw to complete
So-called processing: Once an anomaly occurs, how to get and handle this exception, consisting of two phases, monitoring (try) and capture (catch)

Custom Exception Handler: (Exception handler is an object)

If an exception is thrown, but not captured, it touches a e_error level error that can be handled by the exception of the custom processor to complete the uncaught exception

to create a custom error-handling class :

The 1,myception class inherits the properties and methods of the exception class and is the extension of the original exception class.

2, create the ErrorMessage method, if the email is not valid, the method returns an error

3, execute try code block, due to email illegal, throw an exception

4,catch code block catches an exception and displays an error message

Set_exception_handler () sets a user-defined exception handler

In layman's words: It's simple to call a custom function when your program needs to throw an exception.

PHP error handling and exception handling

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.