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