Home  >  Article  >  Backend Development  >  Comprehensive computer programmers and PHP programmers must also learn to use 'exceptions'

Comprehensive computer programmers and PHP programmers must also learn to use 'exceptions'

WBOY
WBOYOriginal
2016-07-29 08:40:03891browse

The completely open nature of the website determines that the website hopes more than any traditional software to achieve "the system always seems to be working normally", so it is particularly important to adopt the correct method of program error handling. Theoretically, if the design is perfect enough and the developers are careful enough, the possibility of program errors is 0.
But the reality is just the opposite. Complex business logic, different hardware environments, or untrustworthy user input may cause program errors. , the service is down. Therefore, in a slightly complex system, a complete error mechanism is necessary.
Before php5, due to lack of support for exceptions. When doing complex development, the relatively primitive processing form of "processing error values ​​+ recording logs" is often adopted.
For example:

Copy code The code is as follows:


function getResult($a,$b)
{
.....
if fatal error occur
return "error_type1";
.. ...
}
$result = getResult($a,$b);//Theoretically, the getResult function can always return $result correctly
if($result=='error_type1')//But in some special cases .$result cannot be obtained normally
{
writeLog('result is empty!');//record the log
die();//or other more "friendly" processing methods
}


Theoretically, through " The method of "handling error values ​​+ recording log" can also achieve our goal (in fact, this is true. In php3 and php4, there have been many successful and complex enough systems. They even consider all situations, so there is no need to record any logs). But technology will always move forward, not to mention that the vast majority of developers do not have the rigorous and watertight thinking of an outstanding person, so we still have to seriously think about the issue of "how to deal with program errors."
The above "error handling + logging" method has the following disadvantages:
1 If there are too many error conditions, a lot of corresponding error handling codes need to be added, which greatly damages the readability of the program. Your program looks "choppy".
2 If the logic of the program is very complex (for example, the function call of the program is very complex, such as calling getResult() in the getResult2() function, or even more complex multi-level nesting situations), then the error value transfer processing will Make you exhausted. Because in order to ensure that errors can be handled effectively, you must ensure that: The error value is delivered in a lossless manner.
So, change this primitive error handling method. Introducing the exception handling mechanism, you will find gratifying changes:
1 Code readability is greatly enhanced. When developing programs, logical thinking becomes very coherent. In "suspicious" places, you only need to throw an exception. As for how to deal with it, you can wait until later to add it. Of course, readers of the program will not feel interrupted.
2 There is no need to consider the laborious and unpleasant problem of "how to transmit incorrect values ​​without loss". Because of the upward propagation of exceptions, there is no problem if your functions are nested 2, 3, or more layers deep. You only need to have an operation to catch exceptions in the outer layer.
3 Exceptions can be freely customized. You can classify exceptions according to functions to better manage various program errors. At the same time, you can also customize exception handling methods more flexibly. For example, implement the log recording function in the exception class.
Of course, whether to use exceptions depends on your needs. One of the characteristics of PHP is that it is fast to deploy. If it is a small project with simple logic, then using general error value processing methods may be able to deploy faster.

The above has introduced that Cultivation Computer Programmers and PHP programmers must also learn to use "exceptions", including the content of Cultivation Computer Programmers. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn