PDO fetch() 是否在失败时抛出异常?

Does PDO fetch() throw an exception on failure?

如果 PDO 错误报告系统设置为抛出异常,方法 PDOStatement::fetch() 是否会在失败时抛出异常?例如。如果我设置:

PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION

你知道这样的案例吗?

非常感谢。


更新:

方法 PdoStatement::fetch 在失败时抛出异常,而不是 FALSE。这种情况在我的回答中得到了证明:

总结:

a user note on the page you mentioned in your question 表示:

WARNING: fetch() does NOT adhere to SQL-92 SQLSTATE standard when dealing with empty datasets.

Instead of setting the errorcode class to 20 to indicate "no data found", it returns a class of 00 indicating success, and returns NULL to the caller.

This also prevents the exception mechainsm from firing.

Programmers will need to explicitly code tests for empty resultsets after any fetch*() instead of relying on the default behavior of the RDBMS.

I tried logging this as a bug, but it was dismissed as "working as intended". Just a head's up.

根据此说明,您问题的答案是

更新:
接受的答案提到了一个 marked as fixed 错误,错误报告中提供的测试代码不会再引发异常。它实际上早在 2007 年就已修复。

用户 here 声称 fetch() 引发了异常。我会非常小心地假设它不会或不会仅仅因为它们通常在您准备或绑定时抛出而抛出异常。这是将调用放在 try 块中的一个很好的理由。因此,要回答这个问题,在极不可能发生的失败事件中,是的,fetch() 应该抛出异常,在这种情况下确实如此。现在看看是否还有其他情况会很有趣。