Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从PDOException uuu toString方法中删除跟踪信息的最佳方法_Php_Exception_Pdo - Fatal编程技术网

Php 从PDOException uuu toString方法中删除跟踪信息的最佳方法

Php 从PDOException uuu toString方法中删除跟踪信息的最佳方法,php,exception,pdo,Php,Exception,Pdo,是否有可能从_u-toString中去除微量元素 我想要的是这样的东西 class DBException extends PDOException { public function __toString() { return get_class($this) . " '{$this->getMessage()}' in {$this->getFile()}({$this->getLine()})\n"; } } 我试过上述方法,但似乎不起作用。有什么想法吗 如果我

是否有可能从_u-toString中去除微量元素

我想要的是这样的东西

class DBException extends PDOException
{
public function __toString()
{
    return get_class($this) . " '{$this->getMessage()}' in {$this->getFile()}({$this->getLine()})\n";
}
}
我试过上述方法,但似乎不起作用。有什么想法吗

如果我使用下面的try-catch块作为示例,我仍然会得到跟踪数据

try {

// Do something here

}catch(DBException $e) {
    echo $e;

}

我本以为回显$e会触发我的DBException类中的uu-toString方法。

过去我想用PDO处理异常时所做的是,在这种情况下,为了确保不会向用户显示连接详细信息,我扩展了PDO类并简要更改了异常处理程序:

class extendedPDO extends PDO
{
    public static function exception_handler(Exception $exception)
    {
        // Output the exception details
        die('<h1>Database connection error<p>' . $exception->getMessage() . '</p>');
    }

    public function __construct($dsn, $username=null, $password=null, $options=array())
    {
        // Temporarily change the PHP exception handler while we . . .
        set_exception_handler(array(__CLASS__, 'exception_handler'));

        // Create PDO
        parent::__construct($dsn, $username, $password, $options);

        // Change the exception handler back to whatever it was before
        restore_exception_handler();
    }
}
像这样的

public function __toString() {
    $return = "Class: ".get_class($this)
             ."\nMessage: ".$this->getMessage()
             ."\nFile: ".$this->getFile()
             ."\nLine: ".$this->getLine()."\n";
    return $return;
}

请解释这似乎不起作用,这是否意味着对于您的方法,我会抛出PDO异常而不使用try-catch块来确保它们是未捕获的异常?向最终用户显示任何错误信息有什么意义?只显示一个典型的应用程序错误。请稍后再试。消息并将实际错误保存到日志文件中。这些只是示例,我的项目会记录所有错误,并向像您这样的最终用户显示一条通用消息suggest@Crozin在开发过程中,错误消息可能很有用;Shawn,这是从一个特定的示例中获取的,我想确保使用该类的任何人都不会忘记尝试catch而显示连接详细信息,但我想您可以停止恢复,然后看看如何继续。我想这是个糟糕的练习。