php - 自定义PHP错误消息

标签 php sql error-handling

我想更改以下PHP错误消息,以便用户看不到此错误,并且我想显示一个用户更容易理解的错误。

例如,考虑我要编辑的此错误,

Notice: Undefined index: user_input on line 33

我想显示以下错误消息,而不是上面的默认错误消息,
Process failed

我如何在我的网站上实现?

最佳答案

这不是一个错误,而是一个通知,因此您需要做一些事情:

  • 首先查看ErrorException,您需要将通知转换为异常。
  • 捕获异常
  • 打印您的自定义错误消息


  • 例:
    <?php
    function exception_error_handler($errno, $errstr, $errfile, $errline ) {
        throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
    }
    set_error_handler("exception_error_handler");
    
    
    try {
        //Trigger exception:
        $my_array = array();
        echo $my_array['undefined'];//Notice will throw exception
    
    } catch (Exception $e) {
        echo 'Process failed';
    }
    ?>
    

    关于php - 自定义PHP错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32402560/

    相关文章:

    php - 禁用 Laravel 内置的错误处理方法

    php - 句子中句子/单词/字符的出现 - 仅计算单词(如果可能,则不使用正则表达式)(PHP)

    在 Windows 7 中保存 Php 阿拉伯语文件

    php - v3.1.1 的 CakePHP 路由不适用于 v3.1.2,可能是约定错误?

    sql - 使用 PowerShell 将 SQL 的 TCP 端口设置为 1433

    sql - 使用 SQL Server 检索引用树

    php - 函数中的 mysqli_fetch_assoc

    mysql - 如何用 MySQL 中变量的定义值替换变量?

    api - 使用jmeter “too many attempts error”进行API测试

    ios - swift中错误和异常的区别