PHP exception, error handling mechanism

Source: Internet
Author: User
Tags parse error

PHP exception, error handling mechanism


In the actual development, the error and the abnormal catch only rely on Try{}catch () is not enough. Therefore, the following functions are referenced.

Let's start with the exception:

The first thing to understand is that the exception is not the same as the error, the exception is the normal logic outside the situation, and the error refers to the runtime error, for example, using an undefined variable, such as the exception needs to throw (throw) to be caught, and the error will cause the program to terminate the execution

1. The usual way to handle exceptions is to use try{}catch{} to catch exceptions thrown with throw

[PHP] View plaincopy

    1. try{
    2. Throw New Exception ("KKKKKKKKKKKKKKKK");
    3. }
    4. catch (Exception $e) {
    5. Echo $e->getmessage (),"
      ",$e->gettraceasstring ();
    6. }


2, set the exception handling function through the Set_exception_handler function, in this case, even if no try{}catch{},throw thrown exception can be automatically captured by the function set by Set_exception_handler

[PHP] View plaincopy

    1. Set_exception_handler (' Exceptionhandler ');
    2. Throw New Exception ("KKKKKKKKKKKKKKKK");
    3. function Exceptionhandler (Exception $exception) {
    4. Echo $exception->getmessage ();
    5. }


Next we discuss the error:

Usually the program goes wrong and PHP outputs the error message to help debug, but the output of this information can be controlled by the function error_reporting (). Errors in PHP are graded and kind, and the following are descriptions of all the types of errors:
E_all-All errors and warnings (not including e_strict)
E_error-Fatal Run-time error
E_warning-run-time warning (non-fatal error)
E_parse-Compile-time parse error
E_notice-Runtime Reminders (these are often caused by bugs in your code, or by intentional behavior.) )
E_strict-coding normalization warning, which allows PHP to suggest how to modify the code to ensure optimal interoperability forward compatibility.
E_core_error-php fatal error during initialization during startup
E_core_warning-php warnings during initialization during startup (non-fatal error)
E_compile_error-Compile-time fatal error
E_compile_warning-Compile-time warning (non-fatal error)
E_user_error-user-defined error message
e_user_warning-user-defined warning message
E_user_notice-User-definable reminder message

If you set error_reporting (E_notice), then the program will only output e_notice level of information, generally we use only need to set the error_reporting (e_all&! e_warning) on the line.

Above we see a bug called user-defined error message , what is this? Let's look at an example first.

[PHP] View plaincopy

    1. Set_error_handler (' ErrorHandler ');
    2. Trigger_error ("aaaaaaassssssssssss", e_user_error);
    3. function ErrorHandler ($errno,$errstr) {
    4. "White-space:pre" > if ($errno==e_user_error) {
    5. "White-space:pre" > Echo "Innnnnnnni:",$errstr;
    6. "White-space:pre" > }
    7. "White-space:pre" >
    8. }

Output Result:

[PHP] View plaincopy

    1. Innnnnnnni:aaaaaaassssssssssss

Trigger_error () is a function used to throw a user-defined error message, and some of the messages that we can throw custom are treated as errors, such as serious logic problems

The above program we see, when the program error, in addition to let PHP default output error message, we can also set their own error handling function, set the method is Set_error_handler (), see an example

[PHP] View plaincopy

    1. Set_error_handler (' ErrorHandler ');
    2. Echo "dddddddddddd
      ";
    3. Echo $cc; //$CC not defined, Echo will go wrong
    4. function ErrorHandler ($errno,$errstr) {
    5. if ($errno==e_notice) {
    6. Echo "Innnnnnnni:",$errstr;
    7. }
    8. }


Output Result:

Dddddddddddd
innnnnnnni:undefined VARIABLE:CC

The above describes the PHP exception, error handling mechanism, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.