Php simple exception handling and Nested exceptions

Source: Internet
Author: User
PHP5 adds exception handling modules similar to other languages. Exceptions in PHP code can be thrown by throw statements and captured by catch statements. 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, PHP allows another throw exception to be thrown 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.

Note that HP internal functions mainly use error reports, and only modern object-oriented extensions use exceptions. However, errors can be easily converted to exceptions through ErrorException.

The PHP Standard Library (SPL) provides many built-in exception classes.

Example #1 throw an exception

 getMessage(), "\n";    }    // Continue execution    echo 'Hello World';?>

The above routine will output:

0.2
Caught exception: Division by zero.
Hello World

Example #2 Nested exceptions

 getMessage());            }        }    }    $foo = new Test;    $foo->testing();?>

The above routine will output:

String (4) "foo! "

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.