What problem does PHP try catch solve?

Source: Internet
Author: User
Tags php try catch try catch
Know PHP has an exception handling, Baidu a bit, are to tell you how to write grammar. Follow the set of the above as if it will be used. But I haven't found an article to tell you why you want to catch it with a try, and it appears to be a problem to solve. Have seen that the code can be reduced to "if ... else such a wild way", but also did not continue to speak carefully.
Do you know, continue to look for answers, and will often come back to pay attention to the answer to this question.

Reply content:

Know PHP has an exception handling, Baidu a bit, are to tell you how to write grammar. Follow the set of the above as if it will be used. But I haven't found an article to tell you why you want to catch it with a try, and it appears to be a problem to solve. Have seen that the code can be reduced to "if ... else such a wild way", but also did not continue to speak carefully.
Do you know, continue to look for answers, and will often come back to pay attention to the answer to this question.

In addition to processing all the input and calculation results as we wish, it is very important to deal with the result of error input or abnormal operation.

The most primitive exception handling is, of course, the use of the If Control statement, which is too primitive to write a lot of duplicate code, and possibly because the code that handles the exception is too large to hide the bug.

The exception handling mechanism of the senior layer is the wrong mechanism. The error mechanism provides a way to execute a specified code snippet when an exception occurs, and solves the problem of large and unsightly exception handling code that uses if for judgment. However, the error handling mechanism only provides the process of exception handling, does not provide the maintenance of abnormal environment related to stack processing, so the error handling mechanism is only more suitable for the process-oriented and relatively flat programming.

Another layer on the exception handling mechanism, that is, try catch, exception handling, compared to error handling, can preserve and implement stack control, not only can let our exception handling code can be close to the exception of the region can occur, but also allows the programmer to not too much to consider the context of the occurrence of the problem of maintenance. Especially in the stack structure with more layers, the anomaly mechanism can show the structure of the upper layer exception of the stack.

Separate the error codes and the code that handles the errors.

Give the high-level function an opportunity to handle the error.

  • Related Article

    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.