PHP Custom error handling function

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

Basic error handling: Using the Die () function
The first example shows a simple script that opens a text file:

The code is as follows Copy Code
<?php
$file =fopen ("Welcome.txt", "R");
?>

If the file does not exist, you will get an error similar to this:

Warning:fopen (Welcome.txt) [function.fopen]: failed to open stream:
No such file or directory in c:webfoldertest.php on line 2 to prevent users from getting similar error messages, we detect the existence of the file before accessing the file:

The code is as follows Copy Code

<?php
if (!file_exists ("Welcome.txt"))
{
Die ("File not Found");
}
Else
{
$file =fopen ("Welcome.txt", "R");
}
?>

Now, if the file doesn't exist, you get an error message like this:

File not found is more efficient than previous code because it employs a simple error-handling mechanism that terminates the script after the error.

However, simply terminating the script is not always the right approach. Let's look at the alternate PHP functions for handling errors.

Now let's look at a custom error handler

The code is as follows Copy Code

function MyErrorHandler ($errno, $errstr, $errfile, $errline) {
if (!) ( Error_reporting () & $errno)) {return;}
Switch ($errno) {
Case E_USER_ERROR:
echo "<b>my error</b> [$errno] $errstr <br/>";
echo "Error line: $errline in file: $errfile <br/>";
echo "PHP version:". Php_version. " (" . Php_os. ") <br/> ";
Break
Case e_user_warning:
echo "<b>my warning</b> [$errno] $errstr <br/>";
Break
Case E_user_notice:
echo "<b>my notice</b> [$errno] $errstr <br/>";
Break
Default
echo "Unknown error type: [$errno] $errstr <br/>";
Break
}
return true;
}

function Trigger_test ($age) {//Throw error test functions
if ($age <= 0 | | $age > 999) trigger_error ("Age not valid: $age years old", E_user_error);
if ($age <) trigger_error ("Underage: $age-Year-old", e_user_warning);
if ($age > && $age <) trigger_error ("Slightly older: $age years old", E_user_notice);
}
If you are simply handling errors in a simple and uniform way:
$errorHandler = Set_error_handler ("MyErrorHandler");
Trigger_test (1000);//will throw an error level


function Myerror ($errno, $errstr, $errfile, $errline) {
Print_r (Func_get_args ());
Specific processing methods
}
function mywarning ($errno, $errstr, $errfile, $errline) {
Print_r (Func_get_args ());
Specific processing methods
}

function Myntice ($errno, $errstr, $errfile, $errline) {
Print_r (Func_get_args ());
Specific processing methods
}

If you want to handle different error levels separately:

The code is as follows Copy Code
Set_error_handler (' Myerror ', e_user_error);
Set_exception_handler (' mywarning ', e_user_warning);
Set_exception_handler (' Myntice ', e_user_notice);

Trigger_error (' intentionally throwing a mistake, or a very serious one! ', e_user_error);

Below also enclose some PHP error code detailed

Parameters Description
Error_level

Necessary. Sets the error reporting level for user-defined errors. Must be a number of values.

See table below: Error reporting level.

Error_message Necessary. Specify error messages for user-defined errors.
Error_file Optional. Specify the name of the file in which the error occurred.
Error_line Optional. The line number that sets the error to occur.
Error_context Optional. Specify an array that contains each variable that was used when the error occurred and their values.

Error Reporting level
These error reporting levels are different types of errors that the error handlers are designed to handle:

value Constants Description
2 E_warning Non-fatal run-time error. Script execution is not paused.
8 E_notice

Run-time notice.

The script found that an error might occur, but it may also occur when the script is running correctly.

256 E_user_error Fatal user-generated error. This is similar to the e_error that programmers use PHP function Trigger_error () settings.
512 E_user_warning A non-fatal user-generated warning. This is similar to the e_warning that programmers use PHP function Trigger_error () settings.
1024 E_user_notice User-generated notifications. This is similar to the e_notice that programmers use PHP function Trigger_error () settings.
4096 E_recoverable_error A fatal error that can be caught. Similar to E_error, but can be captured by user-defined handlers. (see Set_error_handler ())
8191 E_all

All errors and warnings, except for level e_strict.

(In PHP 6.0,e_strict is part of E_all)

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.