Summary of Common Errors and exceptions in PHP, php Exception Handling

Source: Internet
Author: User
Tags php error php exception handling

Summary of Common Errors and exceptions in PHP, php Exception Handling

Preface

When we develop a program, problems in the program are very common. What should we do when exceptions and errors occur? This article will introduce PHP errors and Exception Handling in detail and share them for your reference. I will not talk much about them below. Let's take a look at the details:

I. PHP error handling

1. Syntax Error

2. runtime error

3. Logic error: no error is prompted, but the function is incorrect, which is the most troublesome.

4. three levels: notice/warning/fatal error (unable to continue execution)

5. Error Report display:

A. You can modify the error_reporting project in php. ini to limit the error report type, for example:error_reporting=E_ALL & ~E_NOTICE

B. Modify only the error display in a script. You can useerror_reporting(E_ALL & ~E_NOTICE); (Recommended)

6. custom error reports:set_error_handler()You can pass in the following predefined parameters to display errors:

Set_error_handler ('reporterror '); $ mess = ""; function reportError ($ error_type, $ error_message, $ error_file, $ error_line) {global $ mess; $ mess. = "the error level is of the {$ error_type} type, and the error information is <B >{$ error_message} </B>. In the file {$ error_file, row {$ error_line. <Br> ";}gettype ($ a); echo" 1111111 <br> "; getType (); echo" 2222 <br> "; echo $ mess; /* the error level is 8. The error information is Undefined variable: a. In the file F: \ projects \ Frame \ FrameTest \ BackEnd \ regularExpression. php, 24th rows. The error level is 2. The error message gettype () expects exactly 1 parameter, 0 given is displayed in the file F: \ projects \ Frame \ FrameTest \ BackEnd \ regularExpression. php, 26th rows. */

7. Record error logs

A. Set display_errors to Off and log_errors to On in PHP. ini.

B. Custom log directoryerror_log="C:/XX/XX/php_error.log"


C. You can also useini_set("display_errors","Off")Or set ini_get in the script.

Ii. PHP Exception Handling

1. try catch. No code can be found in the middle.

2. Exception is a pre-defined class of the system.

3. If an exception object is thrown, the exception object is assigned to the class in catch.

4. After an exception occurs in try, the code will not continue to be executed, but will be directly transferred to catch for execution.

Try {echo "driving to work <br>"; throw new Exception ("the car has a puncture! ");} Catch (Exception $ e) {// equivalent to Exception $ e = new Exception (''); echo $ e-> getMessage (). '<br>'; echo 'replace with a spare tire and continue to work <br> ';}

5. Exception Handling can be used together with error handling

Set_error_handler ('reporterror '); function reportError ($ error_type, $ error_message, $ error_file, $ error_line) {if ($ error_type = E_WARNING) {throw new Exception ("error message: {$ error_message}, error file: {$ error_file}, number of error lines {$ error_line} ") ;}} function drive ($ a) {echo $ ;} try {echo "driving to work <br>"; drive (); // if you forget to pass the parameter, a warning error in the custom error function is triggered and an Exception is thrown.} catch (Exception $ e) {// equivalent to Exception $ e = new Exception (''); echo $ e-> getMessage (). '<br>'; echo "replace with a spare tire and continue to work <br> ";}

6. custom exception classes

A. The Exception class is the base class of all exceptions and does not define the handling methods for specific exceptions (only some methods for getting the prompts)

B. The custom exception class must be a subclass of the system class.

C. If the Exception class is continued, rewrite the constructor. Do not forget to call the parent constructor for initialization.

Class BTException extends Exception {function _ construct ($ message) {parent ::__ construct ($ message) ;}function method () {return "open the trunk and take out the tool, replace the spare tire ";}}try {echo" drive to work <br> "; throw new BTException (" the car has a puncture! ");} Catch (BTException $ e) {// equivalent to Exception $ e = new Exception (''); echo $ e-> getMessage (). '<br>'; echo $ e-> method (). '<br>'; echo "replace with a spare tire and continue to work <br> ";}

7. capture multiple exceptions. Note: try can also be nested.

Class Err1 extends Exception {function _ construct ($ message) {parent ::__ construct ($ message) ;}function method () {return "1 1 1 1 ";}} class Err2 extends Exception {function _ construct ($ message) {parent ::__ construct ($ message) ;}function method () {return "corrected Error 2 ";}} class Err3 extends Exception {function _ construct ($ message) {parent ::__ construct ($ message) ;}function method () {return "corrected error 3 ";}} $ rand = rand (1, 3); try {switch ($ rand) {case 1: throw new Err1 ("error 1"); case 2: throw new Err2 ("error occurred 2"); case 3: throw new Err3 ("error occurred 3") ;}} catch (Err1 $ e) {echo $ e-> getMessage (). '<br>'; echo $ e-> method (). '<br>';} catch (Err2 $ e) {echo $ e-> getMessage (). '<br>'; echo $ e-> method (). '<br>';} catch (Err3 $ e) {echo $ e-> getMessage (). '<br>'; echo $ e-> method (). '<br> ';}

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.