Home > Backend Development > PHP Tutorial > PHP programmers must also learn to use 'exceptions'_PHP Tutorial

PHP programmers must also learn to use 'exceptions'_PHP Tutorial

WBOY
Release: 2016-07-21 14:56:05
Original
824 people have browsed it

 Bangkejia (www.Bkjia.com) Tutorial "PHP programmers, especially those who grew up from php4 or even PHP3, many are not used to using throw Exception handling is an error handling method. Although PHP5 introduced the exception handling mechanism, many PHP programmers still do not really understand and use it.

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 chance of errors in the program is 0.

But the truth is exactly the opposite. Complex business logic, different hardware environments, or untrustworthy user input may cause program errors and service crashes. 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 log" is often adopted.

For example:

以下为引用的内容:
function getResult($a,$b)

{

.......

if fatal error occur

return "error_type1";

.....

}

$result = getResult($a,$b);//理论上,getResult函数总能正确的返回$result

if($result=='error_type1')//但在一些特殊情况.$result无法正常取得

{

writeLog('result is empty!');//记录下log

die();//或者其他更“友好的”处理方式

}

The following is the quoted content:
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 circumstances, $result cannot be obtained normally

{

writeLog('result is empty!');// Record the log

die();//or other more "friendly" processing methods }

Theoretically, our goal can also be achieved by "processing error values ​​+ recording logs" (in fact, this is indeed the case. In php3 and php4, many successful and complex enough systems have appeared, and they even considered to all situations, so there is no need to log anything). 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, the corresponding error handling code needs to be increased a lot, 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 will be transferred Processing can wear you down. 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 original error handling method. Introducing exception handling mechanism, you will find gratifying changes:

 1 Code readability is greatly enhanced. When developing a program, 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 error 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, it may be faster to deploy using the general error value processing method.

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364316.htmlTechArticleLieHuo.Net tutorial PHP programmers, especially those who grew up from php4 or even PHP3 Many programmers are not used to using error handling methods such as throwing exceptions. Although php5 introduced...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template