PHP Exception Handling

Source: Internet
Author: User
Tags php exception handling throw exception

When to consider using an exception mechanism

Error codes and exceptions are methods that return execution results in all development languages, and when writing code, the market needs to consider when to use exception handling and when to consider returning an error code.

    • Error code
      1) The error code is more direct, using code number to represent the result of execution, high performance.
      2) usually in a simple error expression can be considered to use. For example, returning false indicates that the processing failed and the other data represents the correct processing result.
      3) There are a number of processing results that are usually considered in the form of a return array: [' Code ' = ' 001 ', ' msg ' = ' = ' processing failed! '], you can also provide the GetErrorInfo () function, which is used to obtain the error message after processing is complete. This can compensate for the lack of detailed error code return information, but there is still the caller does not necessarily handle the return information, the error code is added, it does not necessarily notify the caller to handle the error message and so on.

    • Exception handling
      1) performance is slightly lower because the program processing needs to enter and leave the stack.
      2) The caller does not miss the newly added error message, and the returned error message is more detailed. Usually the successful execution returns the correct result, and the execution failure returns the exception information.

      Sum up:
      Which way to use the feedback error, also depends on the scene. Personally, the code module with high operating frequency minimizes the use of exception handling, but ensures that the return information is concise. While some failure factors are not exhaustive, consider using exceptions if performance issues do not occur.

PHP Exception handling code
    • Throw exception
thrownewException("error balabalabala...");
    • Catching exceptions
try//If the exception is thrown, this text will not be showncatch(Exception$eecho‘Message: ‘ .$e->getMessage(); }
    • Setting the top level exception processor (top Exception Handler)
      You can set the top-level exception handling class to handle all exceptions.
set_exception_handler(‘myException‘);

PHP Exception Handling

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.