Catch Zend PDO Exception
I'm wondering if I can put a try catch about $application->bootstrap()->run();
pdo exceptions
This works, but it catches every exception, which I do not want.
try {
$application->bootstrap()->run();
} catch (Exception $e) {
print_r($e);
}
I had a nasty incident of pdo exception being throw and displaying the password from application.ini!
Worthy of note, I have tried PDOException, it doesnt get catched.
My DB is set up in bootstrap run() with
try {
$db = $this->getPluginResource('db')->getDbAdapter();
Zend_Registry::set('dbAdapter', $db);
} catch (Exception $e) {
echo 1;exit;
}
Note that if I put in the wrong password locally and run the app, I do not see 1, I see a blank page 开发者_StackOverflow中文版even with error reporting on.
if i understand the question correctly , it mean you are trying to catch you db connection exception
and this would be as easy as these lines below :
try {
$db = Zend_Db::factory('Pdo_Mysql', $parameters);
$db->getConnection();
} catch (Zend_Db_Adapter_Exception $e) {
// perhaps a failed login credential, or perhaps the RDBMS is not running
} catch (Zend_Exception $e) {
// perhaps factory() failed to load the specified Adapter class
}
basically getConnection
function is trying to connect to db with the parameters , if it failed
it would throw an Zend_Db_Adapter_Exception
and if succesfully connected it would return PDO object
similarly , you can use this pattern to catch you Zend_Db
exceptions or PDO_Exceptions
in controller classes or models that throw these kind of errors , but not the whole application
Go where you have the database code and put try catch around that code. If you want only Pdo Exceptions then catch only PdoException. Put something like catch(PdoExcetion_OR_What_Its_Name_Is $e) (And disable the error output to the screen. Write your errors to a log file)
It seems rather strange to try and catch a whole application. Disabling error reporting (such as display_errors in php.ini) would be a lot better to stop revealing any sensitive information from uncatched exceptions.
But to answer your question:
try {
$application->bootstrap()->run();
} catch (PDOException $e) {
print_r($e);
}
This should catch only PDO Exceptions.
精彩评论