What PDO methods throw exceptions?

When you install a new PDO db handler, I have to wrap everything in a try-catch to prevent an error message that will print all db access data to the user.

But what about all the other methods like exec () for example? Should I wrap it all in a try-catch block? At what point is the PHP documentation indicating that the method throws an exception?

+4
source share
2 answers

First of all, you can set how errors are handled by PDOs using the PDO::setAttribute method to set PDO::ATTR_ERRMODE (error report).

In particular, you can configure PDO to throw exceptions when there was an error, instead of the error message, this is what I usually do.


Then, when the method can throw an exception, the documentation should be indicated in it - in general, in the "Return Value" section.

For example, PDO::prepare may throw an exception - depending on the error message (see what I wrote earlier):

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


As a side element: if you find a function / method that throws an exception, and this is not indicated in its documentation, it might be a good idea to create an error report (see http://bugs.php.net/ ), so the problem fixed; -)

(Errors / errors / missing information in the documentation are processed using the bug tracker, like any other error)

+5
source

You can see if the exclusion method excludes by looking at the manual.

If you look at the __ construct manual, you will see below in the Errors / Exceptions section in which it throws an exception.

+2
source

All Articles