捕获 Zend PDO 异常
我想知道是否可以对 $application->bootstrap()->run();
进行 try catch 这将捕获 pdo 异常
这可行,但它捕获了每个异常,这是我不想要的。
try {
$application->bootstrap()->run();
} catch (Exception $e) {
print_r($e);
}
我遇到了一个令人讨厌的事件:抛出 pdo 异常并显示 application.ini 中的密码!
值得注意的是,我尝试过 PDOException,它没有被捕获。
我的数据库是在 bootstrap run() 中设置的,请
try {
$db = $this->getPluginResource('db')->getDbAdapter();
Zend_Registry::set('dbAdapter', $db);
} catch (Exception $e) {
echo 1;exit;
}
注意,如果我在本地输入错误的密码并运行应用程序,我不会看到 1,即使有错误报告,我也会看到一个空白页面。
I'm wondering if I can put a try catch about $application->bootstrap()->run();
that will catch 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 even with error reporting on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我正确理解了这个问题,这意味着您正在尝试捕获数据库连接异常
,这将像下面这些行一样简单:
基本上
getConnection
函数正在尝试使用参数连接到数据库,如果它失败了它会抛出一个
Zend_Db_Adapter_Exception
并且如果成功连接它会返回PDO 对象
类似地,您可以使用此模式来捕获
Zend_Db
异常或PDO_Exceptions
在控制器类或模型中抛出此类错误,但不是整个应用程序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 :
basically
getConnection
function is trying to connect to db with the parameters , if it failedit would throw an
Zend_Db_Adapter_Exception
and if succesfully connected it would returnPDO object
similarly , you can use this pattern to catch you
Zend_Db
exceptions orPDO_Exceptions
in controller classes or models that throw these kind of errors , but not the whole application转到有数据库代码的地方,并在该代码周围放置 try catch。如果您只想要 Pdo 异常,那么只捕获 PdoException。放置类似 catch(PdoExcetion_OR_What_Its_Name_Is $e) 的内容(并禁用屏幕上的错误输出。将错误写入日志文件)
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)
尝试捕获整个应用程序似乎很奇怪。禁用错误报告(例如 php 中的 display_errors .ini)会更好地停止泄露未捕获的异常中的任何敏感信息。
但回答你的问题:
这应该只捕获 PDO 异常。
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:
This should catch only PDO Exceptions.