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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程