【问题标题】:Hide output when an exception is thrown抛出异常时隐藏输出
【发布时间】:2015-01-12 15:42:09
【问题描述】:

我只是想问一下,每当我的脚本遇到异常时,是否有任何方法可以隐藏我已经发送的任何输出。例如:我打印一个数组并遇到异常。此时我只想打印异常消息,而不是数组输出(因为它可能不完整,由于错误)。

【问题讨论】:

  • 注册一个处理所有未捕获异常的异常处理程序。让它清除输出缓冲区,并将异常消息写入其中,然后退出

标签: php exception exception-handling


【解决方案1】:

这种模式可能是你想要的:

// Start output buffering
ob_start();

try {
    // Your code that might throw an error
    // ...

    // No errors: Send output to client
    flush();
    ob_end_flush();
}
catch (\Exception $e) {
    // Error occured. Throw away output and stop buffering
    ob_end_clean();

    // Handle your error
    // ...
}

【讨论】:

    【解决方案2】:

    最简单的方法是缓冲(例如,保存到变量)您的输出并在回显之前检查错误。

    【讨论】:

      猜你喜欢
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 2023-03-05
      • 2017-11-29
      • 1970-01-01
      • 2013-07-24
      • 2019-12-27
      相关资源
      最近更新 更多