dongye8110 2014-12-16 17:00
浏览 67
已采纳

如何捕获require errors + error_handlers

I am trying to use the set_error_handler function to capture require errors.
while my custom error_handler is being used, the context seems completely off, although the trace is correct.

<?php
function error_handler($errno, $errstr, $errfile, $errline){
    throw new Exception($errstr);
}

set_error_handler('error_handler');


try{
    trigger_error("somethign",E_USER_NOTICE);
}catch(Exception $e){
    echo "I got caught";
}


try{
    require "something/that/does/not/exists.php";
}catch(Exception $e){
    echo "I got caught";
}

As can be seen, when I trigger the first error, it triggers the error_handler which in turn is throwing an exception. Which is caught in the first try-catch.

The second time, where I try to include an un-existing file, again the error_handler is used, but the exception is not caught.
What is going on here?

I am using php 5.5.*
Tested in CLI mode.

  • 写回答

2条回答 默认 最新

  • dsafew1231 2014-12-16 17:05
    关注

    Just read the documentation:

    require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script

    (http://php.net/manual/en/function.require.php)

    That means, the script execution is aborted when this error is encountered. Your custom error handler will be used but throw will be ignore because that assumes further execution which is not allowed at this point.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?