Direct judgment in a program
Basic error handling: Using the Die () function
The first example shows a simple script that opens 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 of
The code is as follows |
Copy Code |
<?php
Open a file without doing any processing $FP =fopen ("Aa.txt", "R"); echo "OK"; Processing: To determine whether a file exists file_exists /* if (!file_exists ("Aa.txt")) { echo "file does not exist"; Quit if it doesn't exist Exit (); After exiting, the following code is not executed }else{ $FP =fopen ("Aa.txt", "R"); //... Close after the operation is complete Fclose ($FP); } echo "OK"; */ 3 ways to handle errors in PHP The first: Using simple die statements /* IF (!file_exists ("Aa.txt")) {
Die ("file does not exist ...") "); If it doesn't exist, exit directly. }else{ $FP =fopen ("Aa.txt", "R"); //... Close after the operation is complete Fclose ($FP); } echo "OK"; */ A simpler way File_exists ("Aa.txt") or Die ("file does not exist"); ?> |
Second: Error processor error level handling error mode
The code is as follows |
Copy Code |
<?php // /* Using Error_function (Error_level,error_message, Error_file,error_line,error_context) The function must be capable of handling at least two parameters (Error level and error message). However, you can accept up to five parameters (optional: File, Line-number, and error context): */ //Overwrite Set_error_handler method //If e_warning This error occurs, invoke My_error processing method Set_error_handler ("My_error", e_warning); Set_error_handler ("My_error2", e_user_error); //Setting the corresponding time zone in China date_default_timezone_set (' PRC '); function My_error ($errno, $errmes) { echo "<font size= ' 5 ' color= ' Red ' > $errno </font>"; Output error Reporting level echo "error message is:". $errmes; exit (); } function My_error2 ($errno, $errmes) {
echo "error message is:". $errno, $errmes; Exit (); Enter the error message into the text save the Ready view Use the Error_log () function $message = "error message is:". $errno. " ". $errmes; Error_log (Date ("Y-m-d g:i:s"). " ---". $message." RN ", 3," myerror.txt "); RN for line Wrapping } Open a file without doing any processing $FP =fopen ("Aa.txt", "R"); echo "OK"; Use a custom error to add a trigger this trigger_error () function to specify the calling custom error $age = 200; if ($age >150) { echo "Too old"; To call a trigger and specify an error level here you need to view the Help document Trigger_error ("Not a big problem", e_user_error); Exit (); } ?> |
PHP Exception Handling
PHP 5 provides a new object-oriented error-handling approach
If the exception is not captured and does not use Set_exception_handler () for the appropriate processing, a serious error (fatal error) occurs, and an error message that outputs "uncaught exception" (no exception is caught) is printed.
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 is 1 or below ' in C:webfoldertest.php:6
Stack Trace: #0 c:webfoldertest.php (12):
Checknum #1 {main} thrown in c:webfoldertest.php on line 6Try, throw and catch
To avoid the error in the example above, we need to create the appropriate code to handle the exception.
The processing handler should include:
1.Try-The function that uses the exception should be in the "Try" code block. If no exception is fired, the code continues to execute as usual. However, if the exception is triggered, an exception is thrown.
2.Throw-Here's how to trigger an exception. Each "throw" must correspond to at least one "catch"
3.Catch-"Catch" blocks catch exceptions and create an object that contains exception information
Let's trigger an exception:
The code is as follows |
Copy Code |
<?php To create a function that throws an exception function Checknum ($number) { if ($number >1) { throw new Exception ("Value must be 1 or below"); } return true; } To trigger an exception in a "try" code block Try { Checknum (2); If the exception is thrown, this text won't be shown Echo ' If you are here, the number is 1 or below '; } Catch exception catch (Exception $e) { echo ' message: '. $e->getmessage (); } ?> |
The code above will get an error like this:
Message:value must be 1 or below
To create a custom Exception class
Creating a custom exception handler is simple. We simply created a special class that called its function when an exception occurred in PHP. The class must be an extension of the exception class.
This custom exception class inherits all the properties of the PHP exception class, and you can add custom functions to it.
We started creating 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 (). $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) { br> //throw Exception if email is not valid throw new Customexception ($email); } }< /p> Catch (customexception $e) { //display Custom message echo $e->errormessage (); br> } |
The
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 properties and methods from the old class, and we can use the methods of the exception class, such as Getline (), GetFile (), and GetMessage ().