精彩专题推荐:建站之入门课 建站之必修课 建站之关键课 网站价值所在 流量提高专题 css+div 标准 个人网站打造全过程
返回建站学首页
导航:
建站首页 | 网站设计 | 网站开发 | 网站运营 | 网页软件 | 建站指南 | 搜索优化 | 图像处理 | 视频教程 | 书籍教程 | 建站专题
当前位置:首页>网站开发>PHP教程>正文

学习网页技术PHP中错误处理的一些方法


来源:不详 时间:07-09-17 点击: 点击这里收藏本文




  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 .= ('<hr/>致命错误');
            }
        echo('<pre>');
        echo($str);
        echo('</pre>');

    }
 }
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');


  如果不是上述情况,就不要加了,否则会出现
9 7 3 1 2 3 4 8 :

  把此文章收藏到:          
广而告之
文章搜索
  • Google JZxue.Com

关于我们 | 联系我们 | 友情链接 | 网站地图
Copyright © 2005 - 2006 建站学 All rights reserved.