PHP programmers also learn to use the "exceptions" _php tutorial

Source: Internet
Author: User
   home of the Help (www.Bkjia.com) tutorial"PHP programmers, especially programmers who have grown up from php4 and even PHP3, are not accustomed to using the error-handling method of throwing exceptions. Although PHP5 introduced the exception handling mechanism, many PHP programmers have not really mastered and used it.

The completely open nature of the site determines that the site is more likely than any traditional software to "the system always seems to work properly", so it is important to use the correct procedure error handling. Theoretically, if the design is perfect enough, the developer is cautious enough that the program error may be 0.

But the opposite is true, complex business logic, different hardware environments, or untrusted user input can lead to program errors and service. So in a slightly more complex system, it is necessary to have a perfect error mechanism.

Prior to PHP5, there was a lack of support for exceptions. When doing complex development, we often take the original processing form of "processing error value + log log".

Such as:

The following is the referenced content:
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 not get it properly

{

Writelog (' result is empty! '); /Log The Log

Die ();//or other more "friendly" handling methods

}

In theory, we can achieve our goals by "handling error values + logging log" (in fact, there are many successful and complex systems that have been developed in PHP3,PHP4, and they even consider all the cases, so they don't need to log any logs). But the technology is always going forward, not to mention that most developers do not have the rigorous and watertight thinking of cattle, so we still have to seriously consider "how to deal with program errors" problem.

The "Error handling + Log Log" method above has the following drawbacks:

1 If there are too many error cases, the corresponding error handling code needs to be increased a lot, which is very damaging to the readability of the program. Your program appears to be "intermittent".

2 If the logic of the program is complex (for example, the function call of a program is very complex, such as calling GetResult () in the GETRESULT2 () function, or even a more complex multilevel nesting case), the passing of the wrong value will keep you busy. Because to ensure that errors are handled effectively, you must ensure that the wrong values are passed in a lossless manner.

So, change this primitive way of handling errors. Introducing the exception handling mechanism, you will find gratifying changes:

1 code readability is greatly enhanced. When developing a program, logical thinking becomes coherent, and in "suspicious" places, you just throw an exception. As for how to deal with, can wait until the back to add. Of course, the reader of the program does not feel interrupted.

2 It is no longer necessary to consider the laborious and not flattering question of how error values can be transmitted without loss. Because of the unusual upward passing feature, your function is nested in 2 layers, 3 layers, and no more layers. You just need to have an exception in the outer layer to catch the action.

3 exceptions can be freely customized, you can follow the function of the classification of the exception, better manage the various program errors. You can also customize the handling of exceptions more flexibly. For example, in the exception class to realize the function of log log.

Of course, the use of exceptions depends on the requirements. One of the big features of PHP is that it's fast to deploy, and if it's a small project, the logic is simple, and it might be quicker to deploy with a generic error-handling approach.

Article from: http://www.cnblogs.com/rethink/

http://www.bkjia.com/PHPjc/364316.html www.bkjia.com true http://www.bkjia.com/PHPjc/364316.html techarticle Fire Network (liehuo.net) tutorial PHP programmers, especially those who grew up from PhP4, or even PHP3, are not accustomed to using the error-handling method of throwing exceptions. Although PHP5 introduced ...

  • 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.