当前位置: 主页 > 编程知识 > Php编程 > 动态网页PHP中错误信息的处理方法

动态网页PHP中错误信息的处理方法

时间:2010-1-25来源:站长资讯网 点击:

ok,来看比较oo的处理方式:

class ErrorHandlers extends Exception{

private $_context = null;

function __construct($level, $string, $file, $line, $context=null){

parent::__construct($string,$level);

$this->file = $file;

$this->line = $line;

$this->_level = $level;

$this->_context = $context;

}

function __destruct(){

// parent::__destruct();

}

function Message(){

$errors = array(

E_ERROR => ‘error‘,

E_WARNING => ‘warning‘,

E_PARSE => ‘parsing error‘,

E_NOTICE => ‘notice‘,

E_CORE_ERROR => ‘core error‘,

E_CORE_WARNING => ‘core warning‘,

E_COMPILE_ERROR => ‘compile error‘,

E_COMPILE_WARNING => ‘compile warning‘,

E_USER_ERROR => ‘user error‘,

E_USER_WARNING => ‘user warning‘,

E_USER_NOTICE => ‘user notice‘

);

$str = $errors[parent::getCode()].‘: ‘.parent::getMessage().‘ 在 ‘.parent::getFile().

‘ 的第 ‘.parent::getLine()."行\n" ;

if($this->_level==E_USER_ERROR){

$str .= (‘


 

致命错误‘);

 

}

echo(‘

‘);

 

echo($str);

echo(‘

‘);

 

}

}

function error_handler($errno,$errstr,$errorfile,$errline,$errtext){

throw new ErrorHandlers($errno,$errstr,$errorfile,$errline,$errtext);

}

function exception_handler(Exception $e)

{

$errors = array(

E_ERROR => ‘error‘,

E_WARNING => ‘warning‘,

E_PARSE => ‘parsing error‘,

E_NOTICE => ‘notice‘,

E_CORE_ERROR => ‘core error‘,

E_CORE_WARNING => ‘core warning‘,

E_COMPILE_ERROR => ‘compile error‘,

E_COMPILE_WARNING => ‘compile warning‘,

E_USER_ERROR => ‘user error‘,

E_USER_WARNING => ‘user warning‘,

E_USER_NOTICE => ‘user notice‘);

echo $errors[$e->getCode()].‘: ‘.$e->getMessage().‘ in ‘.$e->getFile().

‘ on line ‘.$e->getLine()."\n";

echo $e->getTraceAsString();

}

trigger_error(‘5do8‘);

try{

$i = 1/0;

} catch(ErrorHandlers $e) {

echo "发生错误."; //可以输出错误行

$e->Message();

}

而后,注意了,如果您第一次(或者重新)加载的话,就加上:

set_error_handler(‘error_handler‘);

set_exception_handler(‘exception_handler‘);

如果不是上述情况,就不要加了,否则会出现

Exception thrown without a stack frame in Unknown on line 0

因为error_handler是anto_flush的。

在一个exception里面不能调用其他的exception。有2条普遍适用的规则,如下:

1:不要在一个Exception里面执行另一个Exception

2:不要在析构函数里面执行Exception.

restore_exception_handler();是可以保存exception柄的,注意,执行error以后就会有Exception的了。

最后,加上一个完整的例子:CallError.php

 

error_reporting(1048);

class ErrorHandlers extends Exception{

private $_context = null;

function __construct($level, $string, $file, $line, $context=null){

parent::__construct($string,$level);

$this->file = $file;

$this->line = $line;

$this->_level = $level;

$this->_context = $context;

}

function __destruct(){

// parent::__destruct();

}

function Message(){

$errors = array(

E_ERROR => ‘error‘,

E_WARNING => ‘warning‘,

E_PARSE => ‘parsing error‘,

E_NOTICE => ‘notice‘,

E_CORE_ERROR => ‘core error‘,

E_CORE_WARNING => ‘core warning‘,

E_COMPILE_ERROR => ‘compile error‘,

E_COMPILE_WARNING => ‘compile warning‘,

E_USER_ERROR => ‘user error‘,

E_USER_WARNING => ‘user warning‘,

E_USER_NOTICE => ‘user notice‘

);

$str = $errors[parent::getCode()].‘: ‘.parent::getMessage().‘ 在 ‘.parent::getFile().

‘ 的第 ‘.parent::getLine()."行\n" ;

if($this->_level==E_USER_ERROR){

$str .= (‘


 

致命错误‘);

 

}

echo(‘

‘);

 

echo($str);

echo(‘

‘);

 

}

}

function error_handler($errno,$errstr,$errorfile,$errline,$errtext){

throw new ErrorHandlers($errno,$errstr,$errorfile,$errline,$errtext);

}

function exception_handler(Exception $e)

{

$errors = array(

E_ERROR => ‘error‘,

E_WARNING => ‘warning‘,

E_PARSE => ‘parsing error‘,

E_NOTICE => ‘notice‘,

E_CORE_ERROR => ‘core error‘,

E_CORE_WARNING => ‘core warning‘,

E_COMPILE_ERROR => ‘compile error‘,

E_COMPILE_WARNING => ‘compile warning‘,

E_USER_ERROR => ‘user error‘,

E_USER_WARNING => ‘user warning‘,

E_USER_NOTICE => ‘user notice‘);

echo $errors[$e->getCode()].‘: ‘.$e->getMessage().‘在‘.$e->getFile().

‘的第‘.$e->getLine()."行\n";

echo $e->getMessage();

die();

}

//

set_error_handler(‘error_handler‘);

//restore_error_handler();

set_exception_handler(‘exception_handler‘);

//restore_exception_handler();

我肯定是错误

?>

执行结果:

notice: Use of undefined constant 我肯定是错误 - assumed ‘我肯定是错误‘在E:\web\web\php\bi\exception\m.php的第74行 Use of undefined constant 我肯定是错误 - assumed ‘我肯定是错误‘

 

另外,在类中,还可以这样:

function trigger_error($error_msg, $error_type = E_USER_WARNING)

{

trigger_error(" error: $error_msg", $error_type);

}

著名的Smarty就是这么做的.

站长资讯网
.
分页: [1] [2]
TAG: PHP 错误信息
推荐内容最近更新人气排行
关于我们 | 友情链接 | 网址推荐 | 常用资讯 | 网站地图 | RSS | 留言