学习动态网页技术PHP中错误处理的一些方法(2)

2018-12-12 22:38

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(\}

著名的Smarty就是这么做的.


学习动态网页技术PHP中错误处理的一些方法(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:二年级上语文教学案例-夕阳真美-苏教版【小学学科网】

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: