- Class Exception
- {
- Protected $ message = 'unknown exception'; // exception information
- Protected $ code = 0; // custom exception code
- Protected $ file; // file name with an exception
- Protected $ line; // The code line number with an exception
- Function _ construct ($ message = null, $ code = 0 );
- Final function getMessage (); // returns exception information
- Final function getCode (); // returns the exception code
- Final function getFile (); // returns the file name with an exception
- Final function getLine (); // return the code line number in which an exception occurs.
- Final function getTrace (); // backtrace () array
- Final function getTraceAsString (); // getTrace () information formatted as a string
- /* Methods that can be reloaded */
- Function _ toString (); // output string
- }
- ?>
Simple example: (throw an error message through an exception)
- Try {
- $ Error = 'My error! ';
- Throw new Exception ($ error)
- } Catch (Exception $ e ){
- Echo $ e-> getMessage ();
- }
2. extend this class
- Class MyException extends Exception
- {
- // Redefine the constructor to make the message a required attribute
- Public function _ construct ($ message, $ code = 0 ){
- // Custom code
- // Make sure all variables are correctly assigned values
- Parent: :__ construct ($ message, $ code );
- }
- // Customize the output style of the string
- Public function _ toString (){
- Return _ CLASS _. ": [{$ this-> code}]: {$ this-> message} \ n ";
- }
- Public function customFunction (){
- Echo "A Custom function for this type of exception \ n ";
- }
- }
The basic idea of exception handling is that the code is called and executed in try code. If an error occurs in the try block, we can execute a process that throws an exception. Some programming languages, such as java, will automatically throw exceptions under certain circumstances. In php, an exception must be thrown manually. Throw an Exception using the following method: Throw new Exception ('message', code); the Throw keyword triggers the Exception handling mechanism, which is a language structure rather than a function, however, you must pass a value to it. It requires an acceptance object. In the simplest case, you can instantiate a built-in Exception class. Finally, after the try code, at least one catch code block must be provided. Multiple catch code blocks can be associated with a try code block. If each catch code block can capture a different type of exception, it makes sense to use multiple catch code blocks. For example, to capture exceptions of the Exception class.
- Catch (Exception $ e)
- {
- // Handing exception
- }
-
The object captured by the Catch code is the object that causes an exception and is passed to the throw statement (thrown by the throw statement ). Exception class instances are a good choice. The Exception class provides the following built-in methods: Getcode ()-return the code passed to the constructor. GetMessage ()-return the message passed to the constructor. GetFile ()-The path to the file that generates the exception code is getLine ()-the row of the code that generates the exception is returned. Note: When an exception is caught, subsequent code in the try () block will not continue to be executed, but will try to find the matched "catch" block. When an exception is thrown and no catch processing is performed, the "Uncaught Exception 'exception'" error is reported.
- Function test ($ val ){
- 'If ($ val> 100 ){
- Throw new Exception ("prompt message: Your input value is too large ");
- }
- }
- Test (111 );
- ?>
3. When an exception is thrown, whether the catch statement block is processed should be treated differently. Code of the user registration function
- Try {
- // Check forms filled in
- If (! Filled_out ($ _ POST )){
- Throw new Exception ('You have not entered the form, please enter it ');
- }
- // Check email address not valid
- If (! Check_email ($ email )){
- Throw new Exception ('invalid email format ');
- }
- // Check whether the density length is greater than 6
- If (strlen ($ passwd <6 )){
- Throw new Exception ('density length should be greater than 6 ');
- }
- // Check whether the two passwords are equal http://bbs.it-home.org
- If ($ passwd! = $ Passwd1 ){
- Throw new Exception ('Two passwords are different. please input them again ');
- }
- // Check whether the length of the user name is correct
- If (strlen ($ username)> 16 ){
- Throw new Exception ('The user name length does not match, please input it again ');
- }
- } Catch (Exception $ e ){
- Echo $ e-> getMessage (); // output exception information.
- }
- ?>
|