PHP Exception Handling

Source: Internet
Author: User

1. Concept of exception:

in the In PHP , exceptions are a very special module. The exception has a unified class called Exception.

An exception is an accident or event that occurs during a program's operation, interrupts the execution of a normal instruction, and jumps to another module to continue execution.

Exception handling is used to change the normal process of a script when a specified error (exception) condition occurs. This condition is called an exception.

2. Class of Exceptions:

(1) built-in classes:

  

(2) the class of the constructor:

  

  

If you use a custom class to extend the built-in exception handling class, and you want to redefine constructor Function , it is recommended to call both parent::__construct () to check that all variables have been assigned values. You can overload __tostring () and customize the style of the output when the object is about to output a string.

3. Exception Example:

Effect:

  

4.Try, throw and catch

(1) Try-the function that uses the exception should be in the "Try" code block. If no exception is triggered, the code will continue to execute as usual. However, if an exception is triggered, an exception is thrown.

(2) Throw-this specifies how the exception is triggered. Each "throw" must correspond to at least one "catch"

(3) Catch-"catch" code block catches an exception and creates an object containing the exception information

Example:

  

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.