Php Exception Handling Mechanism and error handling (1/2)

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

In php, the most common error mechanism we use is try catch {}, which can catch errors easily, however, in php, errors can be viewed and disabled in php. you can add error_display (0) at the beginning of the file in ini to avoid displaying errors.

The Code is as follows: Copy code

<? Php
$ A = fopen('test.txt ', 'R ');
// The file is opened without judgment. If the file does not exist, an error is returned.
?>

The correct statement should be as follows:

The Code is as follows: Copy code

<? Php
If(file_exists('test.txt ')){
Using fw.fopen('test.txt ', 'R ');
// Close after use
Fclose ($ f );
}
?>

I. PHP Error Handling Methods A. Simple die () statements;
Equivalent to exit ();
Example:

The Code is as follows: Copy code

If (! File_exists('aa.txt ')){
Die ('file does not exist ');
} Else {
// Perform the operation
}
// If the die () is triggered, the echo is not executed here.
Echo 'OK ';

Concise Syntax:

The Code is as follows: Copy code

File_exits('aaa.txt ') or die ('file does not exist ');
Echo 'OK ';

B. Custom errors and error triggers

1. Error processor (custom error, usually used for syntax error handling)
Create a custom error function (processor). This function must be able to process at least two parameters (error_level and errormessage), but can accept up to five parameters (error_file, error_line, error_context)
Syntax:

Function error_function ($ error_level, $ error_message, $ error_file, $ error_line, $ error_context)
// After creation, you need to rewrite set_error_handler (); Function
Set_error_handler ('error _ function', E_WARNING); // here, error_function corresponds to the custom processor name created above, and the second parameter is the error level of the custom error processor;

Error Report Level (learn more)

These error reporting levels are different types of errors that the error handler aims to handle:

Value constant description
2 E_WARNING: non-fatal run-time error. Do not pause script execution.
8 E_NOTICE Run-time notification.

An error may occur when the script runs normally.
 
256 E_USER_ERROR fatal user-generated error. This is similar to the E_ERROR set by the programmer using the PHP function trigger_error.
512 E_USER_WARNING non-fatal user-generated warning. This is similar to the E_WARNING set by the programmer using the PHP function trigger_error.
1024 E_USER_NOTICE user-generated notifications. This is similar to the E_NOTICE set by the programmer using the PHP function trigger_error.
4096 E_RECOVERABLE_ERROR: a fatal error that can be captured. Similar to E_ERROR, but can be captured by a user-defined handler. (See set_error_handler ())
8191 all errors and warnings except the level E_STRICT.

(In PHP 6.0, E_STRICT is part of E_ALL)
 

2. Error triggers (generally used to handle logical errors)
Requirement: for example, to receive an age, if the number is greater than 120, it is considered an error.
Traditional method:

'If ($ age> 120 ){
Echo 'Age error'; exit ();
}

Trigger:

'If ($ age> 120 ){
// Trigger_error ('error information' [, 'error level']); the error level is optional and is used to define the error level.
// User-defined levels include E_USER_WARNING, E_USER_ERROR, and E_USER_NOTICE.
Trigger_error ('Age error'); // The default error handling method of the called system. You can also use a custom processor.
}
// Custom processor, same as above
Function myerror ($ error_level, $ error_message ){
Echo 'error text ';
}
// At the same time, you need to change the default processing function of the system.
Set_error_handler ('myerror', E_USER_WARNING); // same as above, the first parameter is the name of the custom function, and the second parameter is the error level. The error level here is generally the following: e_USER_WARNING, E_USER_ERROR, and E_USER_NOTICE]
// Now trigger_error can be used to customize the error handling function.

Exercise questions:

The Code is as follows: Copy code

<? Php
Date_default_timezone_set ('prc ');
Function myerror ($ error_level, $ error_message ){
$ Info = "error code: $ error_leveln ";
$ Info. = "error message: $ error_messagen ";
$ Info. = 'occurrence time: '. date ('Y-m-d H: I: s ');
Using filename='aa.txt ';
If (! $ Fp = fopen ($ filename, 'A ')){
'File creation '. $ filename. 'failed ';
}
If (is_writeable ($ filename )){
If (! Fwrite ($ fp, $ info )){
Echo 'failed to write the file ';
} Else {
Echo 'error message recorded successfully ';
}
Fclose ($ fp );
} Else {
Echo 'file'. $ filename. 'unwriteable ';
}
Exit ();
}
Set_error_handler ('myerror', E_WARNING );
Using fpw.fopen('aaa.txt ', 'R ');
?>

1 2

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.