What is the use of PHP exception handling (Exception)?

Source: Internet
Author: User
Tags error code exception handling php error php exception handling
First look at an official explanation (exception handling), as above, and the last sentence explaining the explanation of the exception to the PHP error associated with the solution.
What are the pros and cons of the exception and the PHP error mechanism? How should it be used?
According to the PHP official description of the error mechanism of PHP (error code) before the exception, used to enter the error message PHP, from the derivative view of the exception this concept can be considered as a custom error mechanism to make up for errors in PHP error mechanism (error code) deficiencies.
This means that a bit of information is convenient, exceptions, can contain a richer error messages, such as error messages, error codes, error lines, files, and even error context, to avoid the problem of not rich error information .
This is the performance of the exception handling performance problems, here are illustrated in the drawings, as follows:
Note: One of the diagrams in the diagram is not rigorous, that is, when determining whether the last catch block is to be judged at the same time (is_a), if it is to enter the last catch block execution.
Performance and convenience, often a contradictory body, according to the scene, to choose their own.
The last one to discuss is the problem of obsessive-compulsive disorder, if all errors are used abnormally, then you will see Mansi try{}catch{}, a lot of errors, have their own decision how to solve. This is the extension requirement for custom exceptions in many frameworks ( too many possible uncaught Exception), and I think it might be possible to give the user the design.

Note: Use try{}catch{} cannot shop warning error, you need to define the function that catches the error, for example:

Set_error_handler (function () {
  & nbsp throw new Exception (' ach! ');
});
 
Try {
    $result = 4/0;
} catch (Exception $e) {
    echo Divide by zero, I don ' t fear you!. Php_eol;
    $result = 0;
}
 
Restore_error_handler ();

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.