PHP Common function Block _ error and exception handling-php (32)

Source: Internet
Author: User
Tags openlog parse error php error php error reporting syslog

First, error and exception handling

1.1 Error types and basic debugging methods
?? Error occurrences of PHP programs are generally attributed to the following three areas:

?? Syntax error:
?? Syntax errors are most common and easy to fix. such as: A semicolon is omitted from the code. Such errors prevent the execution of the script.

?? Run-time error:
?? This error generally does not prevent the PHP script from executing, but it blocks what is currently being done. Output an error, but the PHP script continues to execute

?? Logic error:
?? This error is the most troublesome and does not prevent the script from executing or outputting error messages.
?? An exception is an exception that occurs during the execution of a program, or an event that interrupts the operation of the normal instruction and jumps to the other program module to continue execution.

Error Reporting level for PHP
?? E_all//All information value: 6143
?? e_error//Fatal Run-time error value: 1
?? E_recoverable_error//near-fatal run-time error, if not captured, E_error value: 4096
?? E_warning//Runtime Warning (non-fatal error) value: 2
?? e_parse//compile-time Parse error value: 4
?? E_notice//Runtime reminders (often bugs, may also be intentional) value: 8
?? e_strict//Coding Normalization Warning (recommended how to modify forward-compatible) values: 2048
?? E_core_error fatal error value during initialization of//php at startup:
?? E_core_warning//php Warning (non-fatal error) value during initialization during startup:
?? E_compile_error//compile-time fatal error value:
?? e_compile_warning//compile-time warning (non-fatal error) Value:
?? E_user_error//user-defined fatal error value:
?? E_user_warning//user-defined warning (non-fatal error) Value:
?? E_user_notice//user-defined reminders (often bugs) value: 1024x768

PHP.ini configuration file

display_errors: whether to turn on PHP output error reporting





--can throw any non-noticeable error, default value
Span style= "FONT-SIZE:18PX;" >?? error_reporting= E_error | E_parse | E_core_error
--only considers fatal run-time errors, new parsing errors, and core errors.

--reports all errors except for user-caused errors.

Set Error level instance: error.php

PHP/*turn on the display_errors command in php.ini, only if the command is turned on to output if there is an error report*/Ini_set ('display_errors',1);/*by setting the error_reporting () function in this script, output all levels of error reporting*/error_reporting (e_all);/*The "Note (notice)" Report does not block the execution of the script and is not necessarily a problem*/GetType ($var);//The argument variable supplied when the function was called is not previously declared/*"Warning (warning)" Report, which indicates a problem, but does not prevent execution of the script*/GetType ();//the function was called without providing the necessary parameters/*"Error" report, it terminates the program and the script will no longer execute down*/Get_type ();//call a function that is not defined?>

Configuration directives for PHP error reporting behavior

display_startup_errors= OFF
?? Whether to display errors encountered by the PHP engine during initialization.
?? Log_errors= on
?? Determines the location of log statement records.
?? Error_log (default null)
?? Specifies that the error is written to the file or the log error is logged in the System log syslog.
?? log_errors_max_len=1024
?? The maximum length of each log entry, in bytes. 0 indicates maximum.

1.2 Error log

The error log is logged in two ways:
?? Log error Reporting using the specified file
?? The error log is logged in the operating system's log

Log error Reporting using the specified file

1. Configure PHP.ini First:
?? error_reporting= e_all//will send each error to PHP
?? display_errors=off//do not display error reports
?? log_errors=on//determines where the log statement is recorded.
?? log_errors_max_log=1024//Maximum length per log entry
?? error_log=g:/myerror.log//specifying a file that is incorrectly written into
?? 2, use function: in PHP file using Error_log () to record the log, you can write information to the Myerror.log file
?? such as: Error_log ("Login failed!") ");

2. Use four functions to log logs:
?? Define_syslog_variables ();//Initialize configuration for system log
?? Openlog ();//Open a log link
?? Syslog ();//Send a log

Example

<?PHPif(!Ora_logon ($username, $password)) {Error_log ("Oracle database is not available!",0);//to write an error message to the operating system log}if(! ($foo =Allocate_new_foo ()) {Error_log ("There's a big problem!",1,"[email protected]");//Send to admin mailbox}error_log ("Screwed up!",2,"localhost:5000");//sent to a server that corresponds to a 5000 port on this machineError_log ("Screwed up!",3,"/usr/local/errors.log");//sent to the specified file?>
<? phpdefine_syslog_variables (); Openlog ("PHP5""  The warning reports the demo sent to the syslog, warning time:". Date ("y/m/dh:i:s")); Closelog (); >

View logs: such as Windows systems, you can see the logs in the application options by right-clicking My Computer, selecting management options, and selecting Event Viewer in the System Tools menu.

1.3 Exception Handling

Exception (Exception) processing is used to change the normal process of a script when a specified error occurs. is a new and important feature in the PHP5. Exception handling is an extensible and maintainable mechanism for error handling, and provides a new object-oriented error handling method.
?? Exception handling Format:
try{
Use the try to include code that could potentially cause an exception to occur.
Once an exception is made, try to catch the exception and give it a catch handle.
Throw Exception Statement: Throw exception object.
}catch (Exception object parameter) {
Do the exception handling here.
}[catch (. ,,){
.. .. ..
}]

PHP Common function Block _ error and exception handling-php (32)

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.