Home  >  Article  >  Backend Development  >  关于fatal error的一点疑问

关于fatal error的一点疑问

WBOY
WBOYOriginal
2016-06-23 13:50:41881browse

function dothing(){
$jb->she();
}

dothing();
echo 321;

如上所帖代码,假如运行,想当然得会爆出 fatal error  she 这个方法on a non-object. 打不出后边得 321
现在是 我们不知道 dothing 到底有没有错。dothing对我们来说是个黑匣子。运行完dothing(); 还要echo  321  .

我得问题是,除了以下写法,是否还有更简易的写法来 打出 321

set_error_handler('error');
function dothing(){
$jb->she();
}


dothing();

function error($e){
echo 321;
exit();
}
?>


回复讨论(解决方案)

出错程序终止,后面的不执行。是正常的。
所以需要设置错误处理,当发生错误时,执行对应操作。

出错程序终止,后面的不执行。是正常的。
所以需要设置错误处理,当发生错误时,执行对应操作。



也就是说 拦截fatal error 或者不确定 有没有  fatal error 就必需靠set_error_handler了? 

是的,如果是异常可以用try catch来解决。
但fatal error会终止程序的,try catch不适用。

这种情况则可以用try catch

<?phpfunction dothing(){    $a = 1;    $b = 0;    if($b==0){        throw new  Exception ( 'Division by zero.' );    }}try{    dothing();}catch(Exception $e){    echo 'error:'.$e->getMessage();}echo 321;?>

这种情况则可以用try catch

<?phpfunction dothing(){    $a = 1;    $b = 0;    if($b==0){        throw new  Exception ( 'Division by zero.' );    }}try{    dothing();}catch(Exception $e){    echo 'error:'.$e->getMessage();}echo 321;?>

  不过 fatal error 就不行了。  不过既然知道是的确应该用  set_error_handler拦截 就可以了 。 
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn