PHP error handling method summary

Source: Internet
Author: User
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. directly judge in the program, the basic error handling: Use the die () function... 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.

Directly judge in the program and handle basic errors: using the die () function, the first example shows a simple script to open a text file. the code is as follows:

 

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

For more details, the code is as follows:

 

Method 2: Error Processor error-level error handling method. the code is as follows:

 150) {// echo "too old"; // call the trigger and specify the error level. here, you need to view the help document trigger_error ("This is a bad problem", E_USER_ERROR ); // exit () ;}?>

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 ), the error message "Uncaught Exception" (No Exception is captured) is output.

Let's try to throw an exception without capturing it. the code is as follows:

  1) {        throw new Exception("Value must be 1 or below");    }    return true;}//trigger exceptioncheckNum(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-the abnormal function should be located in the "try" code block. If no exception is triggered, the code will continue to be executed as usual, but if the exception is triggered, an exception will be 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:

 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';} // catch an Exception catch (exception $ e) {echo 'message :'. $ e-> getMessage () ;}?>

The above code will get an error like this: Message: Value must be 1 or below

To create a custom Exception class, it is very simple to create a custom Exception handler. we have simply created a dedicated 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.

We started to create the exception class. the code is as follows:

 getLine() . ' in ' . $this->getFile() . ': ' . $this->getMessage() . ' 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 custom 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 (), getFile (), and getMessage ().


Address:

Reprinted at will, but please attach the article address :-)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.