Use instances to understand PHP5 Exception Handling

Source: Internet
Author: User

Various Errors may inevitably occur in the compilation of a program. How can I handle these errors or display the errors so that we can find the changes? In this case, exception handling is used. Here we will introduce the related concepts of exception handling 5.

PHP5 Exception Handling

PHP 5 adds an exception handling module similar to other languages. Exceptions in PHP code can be thrown by throw statements and captured by catch statements. All codes that require exception handling must be placed in the try code block to capture Possible exceptions.

Each try must have at least one catch corresponding to it. Multiple catch methods can capture exceptions generated by different classes. When the try code block does not throw an exception or the catch Code cannot be found to match the exception thrown, the PHP code will continue to be executed after the jump to the last catch.

Of course, PHP5 Exception Handling allows throw again in the catch code block. When an exception is thrown, subsequent Code (the code block where the exception is thrown) will not continue to be executed, PHP will try to find the first catch that can match it.

If an Exception is not captured and set_exception_handler () is not used for corresponding processing, PHP will generate a serious error and output the Uncaught Exception... (No exception is captured.

Attributes and methods of exception classes for PHP5 Exception Handling 

The following code only describes the structure of the built-in exception handling class. It is not a practical usable code.

 
 
  1. <? Php
  2. Class Exception {
  3. Protected $ message = 'unknown
    Exception ';
  4. // Exception information
  5. Protected $ code = 0;
  6. // Custom Exception Code
  7. Protected $ file;
  8. // File name with exception
  9. Protected $ line;
  10. // The line number of the Code that has encountered an exception
  11. Function _ construct ($ message =
    Null, $ code = 0 );
  12. Final function getMessage ();
  13. // Return exception information
  14. Final function getCode ();
  15. // Return the Exception Code (Code)
  16. Final function getFile ();
  17. // Return the abnormal file name
  18. Final function getLine ();
  19. // Return the abnormal code line number
  20. Final function getTrace ();
  21. // Backtrace () array
  22. Final function getTraceAsString ();
  23. // GetTrace () information formatted into strings
  24. // Reload-able method
  25. Function _ toString ();
  26. // Output string
  27. }
  28. ?>
  29.  

The above is the analysis of PHP5 exception handling, and hope to help friends who need it.


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.