Using examples to understand PHP5 exception handling _php Tutorial

Source: Internet
Author: User
The writing of a program inevitably presents a variety of errors, so how can I handle these errors, or display errors so that we can find the changes? This uses the exception handling. Here we will introduce the concepts related to 5 exception handling.

PHP5 Exception Handling

PHP 5 adds exception handling modules similar to those in other languages. Exceptions generated in PHP code can be thrown by a throw statement and captured by a catch statement. The code that requires exception handling must be placed inside a try code block to catch possible exceptions.

Each try must have at least one catch corresponding to it. You can use multiple catches to catch exceptions that are generated by different classes. When the try code block no longer throws an exception or cannot find a catch that matches the thrown exception, the PHP code resumes execution after jumping to the last catch.

Of course, PHP5 exception handling allows you to throw (throw) exceptions again within a catch code block. When an exception is thrown, the code behind it (the code block where the exception was thrown) will not continue, and PHP will attempt to find the first catch to match it.

If an exception is not captured and does not use Set_exception_handler () for the appropriate processing, then PHP will produce a serious error and output uncaught exception ... (The exception is not caught) prompt information.

The properties and methods of exception class for PHP5 exception handling

The following code is only a description of the structure of the built-in exception-handling class, and it is not a meaningful piece of code available.

 
 
  1. Class exception{
  2. protected $ message = ' Unknown
    Exception ';
  3. Exception information
  4. protected $ Code = 0 ;
  5. User-defined exception codes
  6. protected $file;
  7. The file name of the exception that occurred
  8. protected $line;
  9. The line number of the code where the exception occurred
  10. function __construct ($message =
    NULL , $ Code = 0 );
  11. Final function getMessage ();
  12. Return exception information
  13. Final function GetCode ();
  14. Return exception code (codename)
  15. Final function getFile ();
  16. Returns the file name of the exception that occurred
  17. Final function getLine ();
  18. Returns the line number of the code where the exception occurred
  19. Final function gettrace ();
  20. BackTrace () array
  21. Final function gettraceasstring ();
  22. Gettrace () information that has been rasterized into a string
  23. Methods that can be overloaded
  24. function __tostring ();
  25. A string that can be output
  26. }
  27. ? >

The above is the analysis of PHP5 exception handling, I hope that the need for a friend to help.


http://www.bkjia.com/PHPjc/446145.html www.bkjia.com true http://www.bkjia.com/PHPjc/446145.html techarticle the writing of a program inevitably presents a variety of errors, so how can I handle these errors, or display errors so that we can find the changes? This uses the exception handling. ...

  • 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.