【问题标题】:Handle php errors处理php错误
【发布时间】:2015-03-14 14:43:34
【问题描述】:

我正在尝试捕获php错误并将其作为异常抛出

set_exception_handler(function($e){
 echo $e->getMessage(); 
});

register_shutdown_function(function() {
  $error = error_get_last();
  if ($error['type'] === E_ERROR) {
    throw new ErrorException($error['message'], 0);
  }
});

notExistingFunction();

问题是 ErrorException 没有被我的异常处理程序“捕获”,我得到了

Uncaught exception 'ErrorException' with message 'Call to undefined function notExistingFunction()'

而不是好消息。

【问题讨论】:

  • 您无法捕获关闭处理程序中引发的异常。该处理程序的要点是在终止脚本执行时作为最后的手段调用。

标签: php exception


【解决方案1】:
set_exception_handler( 'ExceptionHandler' );

http://php.net/manual/en/function.set-exception-handler.php

在你的情况下:

set_exception_handler( 'notExistingFunction' );

【讨论】:

    【解决方案2】:
    try{
         //your code
    } catch (Exception $e) {
         //do something with errr
    }
    

    【讨论】:

    • 对不起,这不是我想要达到的目标
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2015-07-05
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多