PHP programmers should also learn to use the "exception" _ PHP Tutorial

Source: Internet
Author: User
PHP programmers should also learn to use "exceptions ". The completely open nature of the website determines that the website is more "The system will always work normally" than any traditional software ", therefore, the use of the correct program error handler's completely open website features determines that the website is more likely to "always work normally as the system looks" than any traditional software ", therefore, it is particularly important to adopt the correct program error handling method. Theoretically, if the design is perfect enough, developers are cautious enough, and the number of errors in the program may be 0.
However, on the contrary, complex business logic, different hardware environments, or untrusted user input may cause program errors and service downtime. Therefore, in a slightly complex system, a complete error mechanism is required.
Before php5, there was a lack of support for exceptions. During complex development, we often adopt the original "processing error value + record log" processing format.
For example:

The code is as follows:


Function getResult ($ a, $ B)
{
.......
If fatal error occur
Return "error_type1 ";
.....
}
$ Result = getResult ($ a, $ B); // theoretically, the getResult function always returns the correct result. $ result
If ($ result = 'Error _ type1') // but in some special cases, $ result cannot be obtained normally.
{
WriteLog ('result is empty! '); // Record the log
Die (); // or other "friendly" processing methods
}


Theoretically, we can achieve our goal through "processing error values + logging" (in fact, in php3 and php4, there have been many successful and complex enough systems. they even consider all the situations and therefore do not need to record any logs ). However, the technology is always moving forward. What's more, most developers do not have the rigorous and thorough thinking, so we have to seriously think about how to handle program errors.
The preceding "error handling + logging" method has the following drawbacks:
1. if there are too many errors, the corresponding error handling code needs to be increased a lot, which seriously damages the readability of the program. Your program looks "intermittent ".
2. if the logic of the program is very complex (for example, the function call of the program is very complicated, such as the call of getResult () in the getResult2 () function, or even more complex multi-level nesting ), the transfer of wrong values will make you exhausted. To ensure that errors can be effectively handled, you must ensure that the error value is transmitted in a non-wear manner.
Therefore, change the original error handling method. Introduce the exception handling mechanism and you will find the following changes:
1. code readability is greatly enhanced. When developing a program, the logic thinking becomes coherent. in a "suspicious" place, you only need to throw an exception. As for how to deal with the problem, you can wait for it to be supplemented later. Of course, readers of the program will not feel interrupted.
2. you no longer need to consider the laborious and thankless problem of "how to transmit error values without loss. Because of the exception passed up, it is no problem that your function is nested with two layers and three layers. You only need to capture exceptions on the outer layer.
3. you can customize exceptions. you can classify exceptions by function to better manage various program errors. At the same time, you can customize exception handling methods more flexibly. For example, the log recording function is implemented in the exception class.
Of course, whether to use exceptions depends on your needs. One of the major features of php is fast deployment. if it is a small project, the logic is very simple, it may be faster to deploy using a common error value processing method.

...

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.