用户定义级别错误处理简易应用
时间:2005-12-21 来源:manbuzhe0301
先定义一个用户错误处理器;
触发错误;
相当简易。
需要说明的是:用户定义错误处理器不能处理如下级别错误:E_ERROR, E_PARSE,
E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR and E_COMPILE_WARNING
// set the error reporting level for this script
error_reporting(E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_USER_ERROR:
echo "严重错误!$errstr[$errno] in $errfile $errline!";
break;
case E_USER_WARNING:
echo "严重警告!$errstr[$errno] in $errfile $errline!";
break;
case E_USER_NOTICE:
echo "通知!$errstr[$errno] in $errfile $errline!";
break;
default:
echo "Unkown error type: [$errno] $errstr
";
break;
}
}
// function to test the error handling
function generate_error($var) {
switch ($var) {
case "1": trigger_error("var 的值为:1", E_USER_ERROR);break;
case "2": trigger_error("var 的值为:2", E_USER_WARNING);break;
case "3": trigger_error("var 的值为:3", E_USER_NOTICE);break;
default : echo "值不为:1,2,3!";
break;
}
}
// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");
generate_error(1);
generate_error(2);
generate_error(44);
?>
触发错误;
相当简易。
需要说明的是:用户定义错误处理器不能处理如下级别错误:E_ERROR, E_PARSE,
E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR and E_COMPILE_WARNING
// set the error reporting level for this script
error_reporting(E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
switch ($errno) {
case E_USER_ERROR:
echo "严重错误!$errstr[$errno] in $errfile $errline!";
break;
case E_USER_WARNING:
echo "严重警告!$errstr[$errno] in $errfile $errline!";
break;
case E_USER_NOTICE:
echo "通知!$errstr[$errno] in $errfile $errline!";
break;
default:
echo "Unkown error type: [$errno] $errstr
";
break;
}
}
// function to test the error handling
function generate_error($var) {
switch ($var) {
case "1": trigger_error("var 的值为:1", E_USER_ERROR);break;
case "2": trigger_error("var 的值为:2", E_USER_WARNING);break;
case "3": trigger_error("var 的值为:3", E_USER_NOTICE);break;
default : echo "值不为:1,2,3!";
break;
}
}
// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");
generate_error(1);
generate_error(2);
generate_error(44);
?>
相关阅读 更多 +