资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

什么是php捕捉错误

这篇文章将为大家详细讲解有关什么是php捕捉错误,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

为蕉岭等地区用户提供了全套网页设计制作服务,及蕉岭网站建设行业解决方案。主营业务为网站建设、成都做网站、蕉岭网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

php捕捉错误的方法:1、使用“try{}catch()”方法捕捉错误;2、通过“set_error_handler”函数捕捉“E_NOTICE”等;3、利用“set_exception_handler”设置默认的异常处理程序。

php错误及异常捕捉

在实际开发中,错误及异常捕捉仅仅靠try{}catch()是远远不够的。

所以引用以下几中函数。

a)   set_error_handler

一般用于捕捉  E_NOTICE 、E_USER_ERROR、E_USER_WARNING、E_USER_NOTICE

不能捕捉:

E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR and E_COMPILE_WARNING。

一般与trigger_error("...", E_USER_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',                
E_STRICT             => 'Runtime Notice',                
E_RECOVERABLE_ERROR  => 'Catchable Fatal Error'                
);    
// set of errors for which a var trace will be saved    
$user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);        
$err = "\n";    
$err .= "\t" . $dt . "\n";    
$err .= "\t" . $errno . "\n";    
$err .= "\t" . $errortype[$errno] . "\n";    
$err .= "\t" . $errmsg . "\n";    
$err .= "\t" . $filename . "\n";    
$err .= "\t" . $linenum . "\n";    
if (in_array($errno, $user_errors)) {        
$err .= "\t" . wddx_serialize_value($vars, "Variables") . "\n";    
}    
$err .= "\n\n";
echo $err;
}
function distance($vect1, $vect2) {    
if (!is_array($vect1) || !is_array($vect2)) {        
trigger_error("Incorrect parameters, arrays expected", E_USER_ERROR);        
return NULL;    
}    
if (count($vect1) != count($vect2)) {        
trigger_error("Vectors need to be of the same size", E_USER_ERROR);        
return NULL;    
} 
for ($i=0; $i

b)   set_exception_handler

设置默认的异常处理程序,用于没有用 try/catch 块来捕获的异常。 在 exception_handler 调用后异常会中止。

与throw new Exception('Uncaught Exception occurred'),连用。

 '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',                
E_STRICT             => 'Runtime Notice',                
E_RECOVERABLE_ERROR  => 'Catchable Fatal Error'                
);    
// set of errors for which a var trace will be saved    
$err = "\n";    
$err .= "\t" . $dt . "\n";    
$err .= "\t" . $errno . "\n";    
$err .= "\t" . $errortype[$errno] . "\n";    
$err .= "\t" . $errmsg . "\n";    
$err .= "\t" . $filename . "\n";    
$err .= "\t" . $linenum . "\n";    
if (1) {        
$err .= "\t" . wddx_serialize_value($vars, "Variables") . "\n";    
}    
$err .= "\n\n";
echo $err;
}
$old_except_handle = set_exception_handler("exceptHandle");
//$t = I_AM_NOT_DEFINED;//generates a warning
$a;
throw new Exception('Uncaught Exception occurred');    
?>

c)   register_shutdown_function

执行机制是:php把要调用的函数调入内存。当页面所有PHP语句都执行完成时,再调用此函数。

一般与trigger_error("...", E_USER_ERROR),配合使用。

";
echo $message;
    //sendemail to
    }
}
?>

c) restore_error_handler()函数

定义和用法 restore_error_handler() 函数恢复之前的错误处理程序,该程序是由 set_error_handler() 函数改变的。

该函数永远返回 true。

是 set_error_handler()的反函数。

关于什么是php捕捉错误就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


文章名称:什么是php捕捉错误
分享地址:http://cdkjz.cn/article/iehisi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220