php错误处理机制
php里面有一套错误处理机制
可以使用set_error_handler接管php错误处理也可以使用trigger_error函数主动抛出一个错误
set_error_handler(error_function,error_types)
参数描述
error_function:规定发生错误时调用的函数.必须
error_type:规定在哪个错误级别会显示用户定义的错误可选,默认"E_ALL"
<?php
function customError($erron,$errstr,$errfile,$errline){
echo "<b>错误代码</b>[${erron}]${errstr}\r\n";
echo "错误所在的代码行:{$errline}";
echo "PHP版本",PHP_VERSION,"(",PHP_OS,")\r\n";
}
set_error_handler("customError",E_ALL|E_STRICT);
$a=array('o'=>2,4,5);
echo $a[0];
自定义错误一定要有四个输入变量$erron,$errstr,$errfile,$errline
erron是一个常量,代表错误的级别
<?php
function customError($errno,$errstr,$errfile,$errline){
//自定义错误处理时,手动抛出异常
throw new Exception($level.'|'.$errstr);
}
set_error_handler("customError",E_ALL|E_STRICT);
try{
$a=5/0;
}catch(Exception $e){
echo '错误信息',$e->getMessage();
}
这样就能捕捉到致命错误
fetal error这样的错误虽然捕捉不到,也无法在发生次错误后恢复流程处理
但是还是可以使用特殊的办法:register_shutdown_function,此函数会在php程序终止或者die时触发一个函数给php一个短暂的回光返照
posted on 2016-01-06 21:30 weipeng007 阅读(68) 评论(0) 收藏 举报
浙公网安备 33010602011771号