Reputation: 4596
I am creating new PDO connection inside class constructor and then use it only in that class. If some error occurs i get fatal error instead of PDOException
.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not fou.....
My code is:
class Helper
{
private $_db;
function __construct($config = FALSE)
{
$this->_db = new PDO($config['database']['dsn'], $config['database']['username'], $config['database']['password'], $config['database']['options']);
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
$config = array(
'database' => array(
'dsn' => 'mysql:host='.$dbhost.';dbname='.$dbname,
'username' => $dblogin,
'password' => $dbpass,
'options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
)
)
);
try {
$helper = new Helper($config);
} catch (PDOException $error) {
die('Database Error: ' . $error->getMessage());
}
So is it possible ?
Upvotes: 1
Views: 125
Reputation: 157909
So is it possible ?
Of course. That's the very purpose of Exceptions actually.
i get fatal error instead of PDOException.
This is not quite true. You actually get an exception, though uncaught one (which, in turn, leads to fatal error).
Regarding your current error - most likely it is caused by some other block of code. There is always a stack trace provided with an exception - so, you can easily find it out. You may catch it there then.
However, I would advise against catching it manually. It would be better to create an exception handler and catch all the exceptions in one place (unless you want to handle error somehow, which is seldom occirred)
Upvotes: 2