SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2010
    Posts
    194
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    should i use 1 or many try catches in a page that uses PDO

    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?

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    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.

    PHP Code:

    try
    {
    // 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.
    Last edited by Serenarules; Aug 8, 2012 at 21:04. Reason: Added additional information.

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2010
    Posts
    194
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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?

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    cp is correct. Typically, the code would look like this...

    PHP Code:

    $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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •