【问题标题】:Php, is there a way to turn all notices/warnings into an exception?Php,有没有办法将所有通知/警告变成异常?
【发布时间】:2015-12-25 15:14:45
【问题描述】:

我已经设置了我的错误处理程序:

set_error_handler (function($errno, $errstr, $errfile,  $errline, array $errcontext) {
  $s = date('Ymd_His');
  switch ($errno)
  {
    case E_USER_ERROR:
      $s.= '_E_';
      break;
    case E_USER_WARNING:
      $s.= '_W_';
      break;
    case E_USER_NOTICE:
      $s.= '_N_';
      break;
    default:
      $s.= '_U_';
      break;
    }
    file_put_contents (APP_PATH_CACHE.'/log'.$s.'_'.rand(1,99999).'.html', print_r(get_defined_vars(), true));
}, E_ALL);

但它可以变成异常吗?这样我就可以看到流程了。

【问题讨论】:

  • 我不明白反对意见。复制自问题10520390?无论如何,赞成...
  • 好吧,对我来说这并不简单。如果我只抛出一个异常,回溯将导致错误处理程序,而不是它的来源。 “关机”中“致命错误”的类似问题
  • @JohnSmith 回溯确实跨越了错误处理程序的位置,但它并没有停在那里,它继续到生成错误的行,见3v4l.org/CpsPS,他们都提到了第 13 行,产生错误的地方。 (至少从 php 5.1 开始,但可能更早,但 cba 测试因为 ErrorException 是在 5.1 中引入的)

标签: php


【解决方案1】:

是的。 这正是创建 ErrorException 的原因。见http://php.net/manual/en/class.errorexception.php

function exception_error_handler($severity, $message, $file, $line) {
    if (!(error_reporting() & $severity)) {
        // This error code is not included in error_reporting
        return;
    }
    throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");

【讨论】:

    【解决方案2】:

    是的,它可以简单地完成。最好的方法是创建自己的类,该类继承自 \Exception 并在这个新类的 error_handler() 抛出对象中。

    之后你也可以用简单的方法来做

    file_put_contents (APP_PATH_CACHE.'/log'.$s.'_'.rand(1,99999).'.html', print_r(get_defined_vars(), true));

    添加

    throw new Exception($errst, $errno); 其中第一个参数是消息,第二个是错误号

    【讨论】:

      猜你喜欢
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 2010-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多