PHP error and exception handling notes

Source: Internet
Author: User
Tags php error

The use of PHP errors and exception handling has finally been made clear, here is a brief note:
0, the principle of procedural processing:

0, the first is the interruption, that is, the rude stop program, and do not provide error information, in PHP is Die () and exit (), just a simple output of a message, than the rude interruption has a certain progress;

1, then the error processing, is to add information about the error on the basis of the interruption, in order to debug, PHP is the default error handling method (see below);

2, in order to compensate for the shortcomings, PHP allows programmers to customize error handling functions;
1. The error handling of PHP itself is unfriendly to web users (warning level errors do not affect subsequent programs, and error and notice level errors terminate the program's operation), and some information about the program is exposed. So the real PHP program is to block all error messages and customize its own processing functions.
2, this custom function is like: handler (int $errno, String $errstr [, String $errfile [, int $errline [, array $errcontext]]), and need to pass Set_error _handler the call. The parameters of the handler function are passed inside of PHP, which is the parameter that was originally passed to the internal error-handling function.
3, note that the Set_error_handler function can be used multiple times, overwriting the last error handler, you can use the Restore_error_handler function to point back to the previous error-handling function

4, corresponding to the Set_exception_handler and Restore_exception_handler functions, the difference is that these two functions are used to handle the exception. Exceptions are defined in PHP5 and are similar to other language try,catch,finally,throw structures. Throw is used to throw an exception, the try is used to detect a piece of code and can detect an exception, which is then handled by the catch structure. The two functions are exception information that is used to process parts of code that are not detected by try.

What is the difference between PHP exceptions and errors?

PHP exceptions are in the PHP5 version, based on exception (all exception base classes) and errorexception two classes.

The following explains why an exception handling mechanism is required:
When a function encounters a return, it ends up running and returns, and the return value is only one, and can be judged accordingly.
However, when new object is newly, even if a part of the class goes wrong, it will continue to run and return a complete object (just like a car with an oil spill, which can not be found in an error)
So in order to see if an error occurs during object formation, an exception handling mechanism is required
!!! Exception handling mechanisms are typically used to handle errors in a class, but can also handle errors in a function

So how does it work in the actual PHP project?

1, set the error level of 0, that is, no user error

2, set the debug identity, the development process for programmers to display error messages for debugging; Display error page to user

3. The settings for error messages are a combination of three functions:
Register_shutdown_function ();
Set_error_handler ();
Set_exception_handler ();

Reference: http://bbs.phpchina.com/thread-212378-1-1.html

Http://yanue.net/post-99.html

PHP error and exception handling notes

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.