开发者

Which PDO methods throw exceptions?

开发者 https://www.devze.com 2022-12-14 18:50 出处:网络
When establishing a new PDO db handler, I\'ve got to wrap everything into a try-catch to prevent an error message that would p开发者_JAVA百科rint all db access data to the user.

When establishing a new PDO db handler, I've got to wrap everything into a try-catch to prevent an error message that would p开发者_JAVA百科rint all db access data to the user.

But how about all the other methods like exec(), for example? Must I wrap all of these into a try-catch block? At which point is the PHP documentation telling that an method throws an exception?


First of all, you can set how errors are dealt with by PDO, using the PDO::setAttribute method, to set the PDO::ATTR_ERRMODE (error reporting) option.

In particular, it is possible to configure PDO so it throws exceptions when there's an error, instead of reporting an "error" -- that's what I generally do.


Then, when a method can throw an exception, it should be indicated in it's documentation -- generaly, it's in the "Return value" section.

For instance, PDO::prepare can throw an exception -- depending on error reporting (see what I wrote just before) :

If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling).


As a sidenote : if you find a function / method that throws an exception, and it's not indicated in its documentation, it might be a good idea to create a bug report (see http://bugs.php.net/ ), so that problem is corrected ;-)

(Errors / mistakes / missing informations in the documentation are treated via the bug-tracker, like any other bug)


You can see if the method throws exceptions by looking at the manual.

If you look at the manual for __construct you'll see at the bottom under Errors/Exceptions that it throws an exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消