sessions — 2012-08-08T17:28:59-04:00 — #1
I have a file with several queries, some selects, some updates, etc. Is it better to have every call to a PDO method, whether it be query(), fetch(), perpare(), or whatever all in an individual try catch or should all such processing go in 1 try catch?
cpradio — 2012-08-08T18:11:19-04:00 — #2
Depends... the way I like to think about it, is will you do something differently if it fails for the query() versus fetch() versus prepare()? If the answer is yes, I'll do something different, then you need multiple try-catches, if your answer is no, I'll just be sending them to an error page, then one will suit you just fine.
serenarules — 2012-08-08T23:51:22-04:00 — #3
To build on what cpradio has already said, when designing your error handling protocols, be mindful that there is a small overhead (however minimal) while instantiating and handling the exception. How much this hurts the performance of your code would entirely depend on the complexity of said code.
If you are bubbling up through several layers of exceptions, passing the previous exception into each new one, then you could run in to less maintainable code, and somewhat less understandable error reports. In most cases, you are going to want to halt execution on the very first true error. In such cases, only a top level try-catch will do the trick.
If you find yourself using them as a means to control program flow, by testing the success of a method, you would be better off testing bool returns and exiting, or controlling the flow in a more formal manner. Using exceptions to notify the user that some method failed is inefficient and considered bad practice.
Remember that some libraries already have an exception subclass they throw when a method fails anyway. The trick here is to read up on those libraries and catch the error by type.
// tried code
catch (PDOException pdoException)
// handle pdo exceptions one way
catch (Exception exception)
// handle everything else another way
My advice would be to start off at the highest level possible and only introduce deeper exceptions if a true, deeper need arises.
In your case, you probably don't want to introduce layered try-catch blocks. However, it may be to your benefit to subclass PDOException into PDOFetchException and PDOQueryException and throw these, letting the top level handler do the work. In this way, you consolidate exception processing without loosing the specific nature you desire. PDOStatement::Execute, for example, returns a bool (as most pass/fail methods do). Test this value and merely raise your subclassed exception.
sessions — 2012-08-09T09:55:23-04:00 — #4
thanks for responses.
so if i use the php code above, what happens if 1 of my several PDO method / db queries were to fail inside the try? Would the program handle the first PDO exception, then stop running? Or would the catch report on every error in the try?
cpradio — 2012-08-09T09:58:32-04:00 — #5
Just the first point in failure, once you fail/throw an exception in a try catch the rest of the try code is skipped to run whatever you have in your catch.
serenarules — 2012-08-09T10:23:28-04:00 — #6
cp is correct. Typically, the code would look like this...
$success = $pdo->Execute($params);
if ($success == false) throw new PdoExecuteException();
If failure is due to success being false (even though Execute concluded correctly) you can raise your own exception. If failure is due to something internal to PDO, the base PdoException is thrown. The latter should rarely happen after you've debugged the app, unless the user seriously goobers up the product installation.