【问题标题】:How to hide PHP error when using try catch使用try catch时如何隐藏PHP错误
【发布时间】:2019-06-16 14:10:25
【问题描述】:

我怀疑 PHP 的 try catch 机制为什么我使用 try catch 处理错误,但 PHP 仍然显示默认错误消息

例如,我正在使用 PHP excel lib 来解析 xls 文件,但我想打印一个用户友好的错误输出,比如

{code:1, msg:"some error"}

所以我做这些:

    try {
        return $this->getJSON($fileName);
    }catch (Exception $e){
        $f = fopen('/' . trim(ROOT_DIR, '/\\') . '/file/Excel_error', 'wb');
        fwrite($f, $e);
        $split_content = "\n\n-------------------------------------\n\n";
        fwrite($f, $split_content);
        fclose($f);
        echo json_encode(array(
            "code" => 10000,
            "msg" => "Excel parse error",
            "files"=>null
        ));
    }

我想记录错误文件并将我的自定义消息输出到前端,但它不起作用,当我触发错误/警告或其他时,PHP 仍然显示PHP Error/E_WARINING .. . 喜欢按照截图

PS:我处理getJSON()中的文件,这个函数是我的入口。

【问题讨论】:

标签: php try-catch phpexcel


【解决方案1】:

您可以更改php.ini 文件中的错误处理和日志记录部分。您可以将 error_reporting 的值更改为 E_ERROR 以减少报告的详细程度并避免显示警告。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 2011-02-15
    相关资源
    最近更新 更多