SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Location
    Kansas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Throwing Exceptions Question

    I'm in the process of writing a class and I'm come to realize that I am going to need to catch other exceptions rather than just PDO exceptions. Right now my code looks similar to the following.

    PHP Code:
    try
    {
        
    $sPageEditSQL "a query here";
        
    $stmt $this->dbObj->prepare($sPageEditSQL);
        
    $stmt->bindValue(':custId'$_SESSION['custId']);
        
    $stmt->execute();
        
            while( 
    $pageResults $stmt->fetch(PDO::FETCH_ASSOC))
            {
                
    $body .= $pageResults['PageName']."<br/>\n";
            }
        
    }
    catch (
    PDOException $error)
    {
        
    $sErrorMsg $error->getMessage();
        
    $this->errObj->logErrorToFile2($sErrorMsg,$sPageEditSQL);
        
    $body .= "<p class=\"errorMessage\">An error occured when trying to retrieve a list of your pages.<br />If this error continues to happen please contact support.</p>";

    Now lets say I do this in place of the catch statement above.

    PHP Code:
    catch (Exception $error
    Will it still catch the PDO exceptions as well?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Yes it will. PDOException extends from Exception, and any other that extends from it will also be caught.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Location
    Kansas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Yes it will. PDOException extends from Exception, and any other that extends from it will also be caught.
    Awesome! Good to know. Thanks.


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
  •