Php exception handling

Source: Internet
Author: User
Php error handling 1. view the source code of the error type and copy it to the clipboard to print help; E_ALL & nbsp; //-all errors and warnings (excluding E_STRICT ). & nbsp; E_ERRO php error handling
I. types of errors

View source code Copy to clipboard printing help
; E_ALL //-all errors and warnings (excluding E_STRICT ).
; E_ERROR //-fatal runtime error
; E_RECOVERABLE_ERROR //-almost fatal runtime error
; E_WARNING //-warning during running (non-fatal error)
; E_PARSE //-parsing error during compilation
; E_NOTICE //-runtime prompts, which are often caused by bugs in the code.
; E_STRICT //-runtime prompt, which can give PHP suggestions to improve your code.
; E_CORE_ERROR //-fatal error during PHP initialization startup.
; E_CORE_WARNING // non-fatal error during PHP initialization startup.
; E_COMPILE_ERROR //-fatal compilation error.
; E_COMPILE_WARNING //-compilation error (non-fatal error ).
; E_USER_ERROR //-user error message.
; E_USER_WARNING //-user warning information.
; E_USER_NOTICE //-user prompt information .;
II. display of php. ini configuration errors

View source code Copy to clipboard printing help
// 1. the error message is output to the client (this is more convenient when debugging code)
Display_errors = ON

// 2. the error message will not be output to the user end (remember to change it to off when it is finally published to the user)
Display_errors = OFF
3. php. ini error output configuration recommendation

View source code Copy to clipboard printing help
// 1. output all information.
Error_reporting = E_ALL

// 2. output all errors except the prompt.
Error_reporting = E_ALL &~ E_NOTICE

// 3. all ERROR messages are output.
Error_reporting = E_COMPILE_ERROR | E_RECOVERABLE_ERROR | E_ERROR | E_CORE_ERROR
4. complete error levels in php

1. error level list

Value error code
1 E_ERROR
2 E_WARNING
4 E_PARSE
8 E_NOTICE
16 E_CORE_ERROR
32 E_CORE_WARNING
64 E_COMPILE_ERROR
128 E_COMPILE_WARNING
256 E_USER_ERROR
512 E_USER_WARNING
1024 E_USER_NOTICE
2047 E_ALL
2048 E_STRICT
4096 E_RECOVERABLE_ERROR
2. use error_reporting ([int level]) to adjust the error level

Example

View source code Copy to clipboard printing help
Error_reporting (0); // disable all ERROR DISPLAY

Error_reporting (E_ERROR | E_WARNING | E_PARSE );

Error_reporting (E_ERROR | E_WARNING | E_PARSE | E_NOTICE );

Error_reporting (E_ALL ^ E_NOTICE); // php. ini error value.

Error_reporting (E_ALL); // displays all error messages.
V. Custom Error function processing

1. use the set_error_handler () function to declare an error handling function to the system.

2. trigger_error () function can trigger an error

Example

View source code Copy to clipboard printing help
Function myErrorHandler ($ errno, $ errstr, $ errfile, $ errline)
{
Switch ($ errno ){
Case E_USER_ERROR:
Echo" My ERROR[$ Errno] $ errstr \ n ";
Echo "Fatal error in line $ errline of file $ errfile ";
Echo ", PHP". PHP_VERSION. "(". PHP_ OS. ") \ n ";
Echo "Aborting... \ n ";
Exit (1 );
Break;
Case E_USER_WARNING:
Echo" My WARNING[$ Errno] $ errstr \ n ";
Break;
Case E_USER_NOTICE:
Echo" My NOTICE[$ Errno] $ errstr \ n ";
Break;
Default:
Echo "Unknown error type: [$ errno] $ errstr \ n ";
Break;
}
}

// Define the error handling function.
Set_error_handler ("myErrorHandler ");

// Assume that a negative age occurs, which is obviously incorrect.
$ Age =-100;

If ($ age <0 ){
// Trigger an error;
Trigger_error ("age you input must> = 0", E_USER_ERROR );
}

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.