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.
- <? Php
- Class Exception {
- Protected $ message = 'unknown
Exception ';
- // Exception information
- Protected $ code = 0;
- // Custom Exception Code
- Protected $ file;
- // File name with exception
- Protected $ line;
- // The line number of the Code that has encountered an exception
- Function _ construct ($ message =
Null, $ code = 0 );
- Final function getMessage ();
- // Return exception information
- Final function getCode ();
- // Return the Exception Code (Code)
- Final function getFile ();
- // Return the abnormal file name
- Final function getLine ();
- // Return the abnormal code line number
- Final function getTrace ();
- // Backtrace () array
- Final function getTraceAsString ();
- // GetTrace () information formatted into strings
- // Reload-able method
- Function _ toString ();
- // Output string
- }
- ?>
-
The above is the analysis of PHP5 exception handling, and hope to help friends who need it.