The use of PHP custom error handling functions _php example

Source: Internet
Author: User

Copy Code code as follows:

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:
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);

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.