PDO方法会失败并且不会引发PDOException吗? [英] Can PDO methods fail and not throw PDOException?
问题描述
我正在调整为PHP/5.2编写的旧数据库类,该类旨在连接到MySQL并隐藏所有错误.我已将PDO实例配置为引发异常:
I'm tweaking a legacy database class written for PHP/5.2 that was designed to connect to MySQL and hide all errors. I've configured the PDO instance to throw exceptions:
new PDO($dsn, $user, $pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION))
现在,我需要相应地调整代码,以处理函数可能会抛出异常的情况,因此,请阅读手册.
Now I need to adjust my code accordingly to handle the case where functions can throw an exception where they wouldn't before so I head to the manual.
在某些情况下,该手册是明确的,例如PDO::prepare
:
In some cases the manual is explicit, e.g. PDO::prepare
:
如果数据库服务器成功准备了该语句,
PDO::prepare()
返回PDOStatement
对象.如果数据库服务器 无法成功准备语句,PDO::prepare()
返回FALSE
或发出PDOException
(取决于错误处理).
If the database server successfully prepares the statement,
PDO::prepare()
returns aPDOStatement
object. If the database server cannot successfully prepare the statement,PDO::prepare()
returnsFALSE
or emitsPDOException
(depending on error handling).
在其他情况下,它有点含糊不清,例如PDO::commit
:
In other cases it's kind of vague, e.g. PDO::commit
:
成功返回
TRUE
或失败返回FALSE
.
抛出PDOException
如果没有有效的交易.
Returns
TRUE
on success orFALSE
on failure.
Throws aPDOException
if there is no active transaction.
(它是否可能由于其他原因而失败,仅返回false
?)
(Can it fail for some other reason and simply return false
?)
在某些情况下,甚至没有提到例外情况,例如PDO::bindValue
:
And there're cases when exceptions are not even mentioned, e.g. PDO::bindValue
:
成功返回
TRUE
,失败返回FALSE
.
...即使可以确认是否确实在错误(至少在某些错误)上抛出了PDOException
.
... even though it's clear to verify that it does throw PDOException
on error (at least, on certain errors).
如果手册中没有其他说明,我是否仍需要检查在错误时返回false
的方法的返回值?
Do I still need to check the return value of methods that return false
on error when the manual doesn't say otherwise?
推荐答案
不,当您设置PDO::ERRMODE_EXCEPTION
时,总是会出现任何错误的异常
No, when you set PDO::ERRMODE_EXCEPTION
there will be always exception for any errors
这篇关于PDO方法会失败并且不会引发PDOException吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!