PHP programmers also learn to use the "exception" _php technique

Source: Internet
Author: User
The site is completely open to the characteristics of the site, the decision is more than any traditional software would like to do "the system seems to always be able to work properly", so it is particularly important to use the correct method of error handling. Theoretically, if the design is perfect enough, the developer is cautious enough that the program may be 0 wrong.
But the opposite is true, complex business logic, different hardware environments, or untrusted user input can cause programs to go wrong and serve as machines. So in a slightly more complex system, it is necessary to have a perfect error mechanism.
Before PHP5, there was a lack of support for exceptions. When doing complex development, often take the form of the original "handle error value + log log" processing.
Such as:
Copy Code code as follows:

function GetResult ($a, $b)
{
.......
If fatal error occur
return "Error_type1";
.....
}
$result = GetResult ($a, $b);//Theoretically, the GetResult function always returns correctly $result
if ($result = = ' error_type1 ')//But in some special cases. $result cannot be obtained normally
{
Writelog (' result is empty! '); /log under Log
Die ()//or other more "friendly" way of handling
}

In theory, we can achieve our goal by "Handling error values + log log" (in fact, there are many successful and complex systems in PHP3,PHP4, they even take into account all the circumstances, so they don't need to log any log). But the technology must move forward, not to mention, most of the developers do not have the rigor of the bull to the watertight thinking, so we still have to seriously think about "how to deal with procedural errors" problem.
The above "error handling + log Log" approach, there are the following drawbacks:
1 If there are too many error cases, the corresponding error handling code needs to be increased, which is very damaging to the readability of the program. Your program appears to be "intermittent."
2 If the logic of a program is complex (such as the complexity of a program's function call, as in the case of a call to GetResult () in the GETRESULT2 () function, or even a more complex multilevel nesting scenario), the error-handling process can make you tired. Because in order to ensure that errors are handled effectively, you must ensure that the error values are passed in a lossless manner.
So, change this primitive way of dealing with errors. By introducing an exception handling mechanism, you will find gratifying changes:
1 code readability is greatly enhanced. When you develop a program, logical thinking becomes coherent, and in "suspicious" places you just throw an exception. As to how to deal with, can wait until later to supplement. Of course, the reader of the program will not feel interrupted.
2 It is no longer necessary to consider the laborious and less flattering question of how the error value is delivered without loss. Because of the nature of the exception, your function is nested in a 2-layer, 3-layer, no matter how many layers. All you have to do is catch an exception in the outer layer.
3 exceptions can be freely customized, you can according to the function of the exception classification, better management of various program errors. At the same time you can also be more flexible to customize the handling of the exception. For example, in the exception class to achieve log log functions.
Of course, the use of exceptions depends on the requirements. One of the features of PHP is that it's quick to deploy, and if it's a very small project, the logic is simple, and it might be quicker to deploy using a generic error-handling 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.