What problem does PHP's try catch solve?

WBOY
Release: 2016-10-22 00:14:21
Original
1218 people have browsed it

I know that PHP has exception handling. I checked it on Baidu and they all told you how to write the syntax. Just follow the instructions above and assume you know how to use it. But I haven’t found an article that tells you why you should use try catch and what problem it appears to solve. I've seen it said that it can reduce "if...else and other wild ways" in the code, but I didn't go into details.
If you know anything, continue to look for the answer, and will come back often to pay attention to the answer to this question.

Reply content:

I know that PHP has exception handling. I checked it on Baidu and they all told you how to write the syntax. Just follow the instructions above and assume you know how to use it. But I haven’t found an article that tells you why you should use try catch and what problem it appears to solve. I've seen it said that it can reduce "if...else and other wild ways" in the code, but I didn't go into details.
If you know anything, continue to look for the answer, and will come back often to pay attention to the answer to this question.

In addition to processing all inputs and calculation results according to our wishes, the program also handles those incorrect inputs or abnormal operation results. It is also a very important link.

The most primitive exception handling is of course the use of the If control statement you mentioned. This method is too crude. Not only does it require writing a lot of repeated code, but the code for handling exceptions may also be too large, causing bugs to be hidden in it.

The higher-level exception handling mechanism is the error mechanism. The error mechanism can provide a method to execute specified code segments when an exception occurs, solving the problem of a large number of bloated and ugly exception handling codes that use If for judgment. However, the error handling mechanism only provides the process of exception handling, and does not provide maintenance related to exception environments such as stack processing. Therefore, the error handling mechanism is only more suitable for relatively flat programming such as process-oriented programming.

The next level up is the exception handling mechanism, which is Try Catch. Compared with error handling, exception handling can retain and implement stack control, which not only allows our exception handling code to keep close to the area where exceptions may occur, It also saves programmers from having to think too much about context maintenance when exceptions occur. Especially in stack structures with many levels, the exception mechanism can well display the upper-layer exception handling structure of the lower layer of the stack.

Separate error reporting code and error handling code.

Give high-level functions a chance to handle errors.

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!