There are many error handling methods in php, especially after php5, special php processing classes are provided. Below I have collected some methods and programs for PHP error handling and shared them with you.
Direct judgment in the program
Basic Error Handling: Use the die () function
The first example shows a simple script for opening a text file:
The Code is as follows: |
Copy code |
<? Php $ File = fopen ("welcome.txt", "r "); ?> |
If the file does not exist, you will get an error similar to this:
Warning: fopen(welcome.txt) [function. fopen]: failed to open stream:
No such file or directory in C: webfoldertest. php on line 2
More details
The Code is as follows: |
Copy code |
<? Php
// Open a file without any processing // $ Fp = fopen ("aa.txt", "r "); // Echo "OK "; // Process: Determine whether the file exists file_exists /* If (! File_exists ("aa.txt ")){ Echo "the file does not exist "; // Exit if it does not exist Exit (); // after exiting, the following code will not be executed } Else { $ Fp = fopen ("aa.txt", "r "); // Close after the operation. Fclose ($ fp ); } Echo "OK "; */ // Three methods for PHP to handle errors // Method 1: Use a simple die statement /* If (! File_exists ("aa.txt ")){
Die ("the file does not exist... "); // Exit directly if it does not exist } Else { $ Fp = fopen ("aa.txt", "r "); // Close after the operation. Fclose ($ fp ); } Echo "OK "; */ // A Simpler Method File_exists ("aa.txt") or die ("file does not exist "); ?>
|
Method 2: Error processor error level error handling
The Code is as follows: |
Copy code |
<? Php // /* Use error_function (error_level, error_message, Error_file, error_line, error_context) This function must be able to process at least two parameters (error level and error message ), However, you can accept up to five parameters (Optional: file, line-number, and error context ): */ // Rewrite the set_error_handler Method // If the error E_WARNING occurs, call the my_error processing method. Set_error_handler ("my_error", E_WARNING ); Set_error_handler ("my_error2", E_USER_ERROR ); // Set the time zone corresponding to China Date_default_timezone_set ('prc ');
Function my_error ($ errno, $ errmes ){
Echo "<font size = '5' color = 'red'> $ errno </font>"; // Output Error Report Level Echo "error message:". $ errmes; Exit (); } Function my_error2 ($ errno, $ errmes ){
// Echo "error message:". $ errno, $ errmes; // Exit (); // Input the error information to the text and save it as ready. view it and use the error_log () function. $ Message = "error message:". $ errno. "". $ errmes; Error_log (date ("Y-m-d G: I: s "). "---". $ message. "rn", 3, "myerror.txt"); // rn indicates line feed } // Open a file without any processing // $ Fp = fopen ("aa.txt", "r "); // Echo "OK "; // Use the trigger_error () function of the custom error to add the trigger to specify to call the custom error $ Age = 200; 'If ($ age> 150 ){ // Echo "too old "; // Call the trigger and specify the error level. view the help document here. Trigger_error ("this is a bad problem", E_USER_ERROR ); // Exit (); } ?>
|
PHP Exception Handling
PHP 5 provides a new object-oriented error handling method.
If the exception is not captured and set_exception_handler () is not used for corresponding processing, a serious (fatal) error will occur ), and output the "Uncaught Exception" (No Exception is caught) error message.
Let's try to throw an exception without capturing it:
The Code is as follows: |
Copy code |
<? Php // Create function with an exception Function checkNum ($ number) { If ($ number> 1) { Throw new Exception ("Value must be 1 or below "); } Return true; } // Trigger exception CheckNum (2 ); ?> |
The above code will get an error similar to this:
Fatal error: Uncaught exception 'exception'
With message 'value must be 1 or below 'in C: webfoldertest. php: 6
Stack trace: #0 C: webfoldertest. php (12 ):
CheckNum (28) #1 {main} thrown in C: webfoldertest. php on line 6Try, throw, and catch
To avoid errors in the above example, we need to create appropriate code to handle exceptions.
The processing process should include:
1. Try-abnormal functions should be located in the "try" code block. If no exception is triggered, the Code continues as usual. However, if an exception is triggered, an exception is thrown.
2. Throw-This specifies how to trigger an exception. Each "throw" must correspond to at least one "catch"
3. the Catch-"catch" code block captures exceptions and creates an object containing exception information.
Let's trigger an exception:
The Code is as follows: |
Copy code |
<? Php // Create a function that can throw an exception Function checkNum ($ number) { If ($ number> 1) { Throw new Exception ("Value must be 1 or below "); } Return true; } // Trigger an exception in the try code block Try { CheckNum (2 ); // If the exception is thrown, this text will not be shown Echo 'if you see this, the number is 1 or below '; } // Capture exceptions Catch (Exception $ e) { Echo 'message: '. $ e-> getMessage (); } ?> |
The above code will get an error similar to this:
Message: Value must be 1 or below
Create a custom Exception class
It is very easy to create a custom exception handler. We have simply created a special class. When an exception occurs in PHP, we can call its function. This class must be an extension of the exception class.
This custom exception class inherits all attributes of the PHP exception class. You can add custom functions to it.
Let's start to create the exception class:
The Code is as follows: |
Copy code |
<? Php Class customException extends Exception { Public function errorMessage () { // Error message $ ErrorMsg = 'error on line'. $ this-> getLine (). 'in'. $ this-> getFile () . ': <B>'. $ this-> getMessage (). '</B> is not a valid E-Mail address '; Return $ errorMsg; } } $ Email = "someone @ example...com "; Try { // Check if If (filter_var ($ email, FILTER_VALIDATE_EMAIL) === FALSE) { // Throw exception if email is not valid Throw new customException ($ email ); } } Catch (customException $ e) { // Display m message Echo $ e-> errorMessage (); } ?> |
This new class is a copy of the old exception class, plus the errorMessage () function. Because it is a copy of the old class, it inherits attributes and methods from the old class. We can use methods of the exception class, such as getLine () and getFile () and getMessage ().