引发异常时隐藏输出


Hide output when an exception is thrown

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

此模式可能是您想要的:

// 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
    // ...
}

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