Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 530241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:10:34+00:00 2026-05-13T09:10:34+00:00

Example: I use a PDO method that throws an exception. But inside the try

  • 0

Example: I use a PDO method that throws an exception. But inside the try block, I also throw an own Exception if something else goes wrong. Now I want to handle the PDOException and my own Exception separately. How could I separate this to make it work?

   public function prepare(string $sql, array $params) {
    try {
        $prepared = $this->dbh->prepare($sql); // may throw PDOException
        $i = 1;
        foreach ($params as $param) {
        $ok = $prepared->bindParam($i, $param);
        if (!$ok) {
            throw new Exception("Unable to bind parameter (" . $param . ") to prepared statement: " . $sql);
        }
        $i++;
        }
    } catch (PDOException $e) {
        throw new Exception("DB:prepare(): Could not prepare statement. " . $e->getMessage());
    }
    return $prepared;
   }

Please note that I have a global error handler that takes care of logging any exception to a file. That’s why I throw my own exceptions instead of writing directly to a log file (that way, I get also the backtrace).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:10:35+00:00Added an answer on May 13, 2026 at 9:10 am

    You can use several catch blocks, like this :

    try {
        // ...
    } catch (PDOException $e) {
        // Handle the PDOException, with some specific code
    } catch (Exception $e) {
        // Handle any other kind of Exception 
        // that has not already been catched by 
        // one of the previous(es) catch block
    }
    

    But note you have to put the “most specific” exception type first — which means Exception has to be the last one.

    Here are a couple of articles / blog-posts that might interest you, about exceptions and PHP :

    • Exceptional PHP: Introduction to Exceptions
    • Exceptional PHP: Extending The Base Exception Class
    • Exceptional PHP: Nesting Exceptions In PHP
    • PHP Advent 2009 / Exceptional PHP
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 512k
  • Answers 512k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The problem is with your printf statements. The UIAcceleration class… May 16, 2026 at 5:30 pm
  • Editorial Team
    Editorial Team added an answer This works because Windows doesn't allow mapping pages for the… May 16, 2026 at 5:30 pm
  • Editorial Team
    Editorial Team added an answer It looks like you need to update Visual Studio's configuration… May 16, 2026 at 5:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Sometimes one has to refer to another method when commenting. Here some example in
I'm trying to use this library : http://pastebin.com/xgPXpGtw (an example of use: http://pastebin.com/fNFAW3Fh )
How can TFS 2010 improve my development process? For example, I use shelvesets to
In Ubuntu, for example, you can use Super-M to invert the screen colors (requires
I am trying to paginate the results of an SQL query for use on
Currently I'm programming a database class which makes a little bit use of PHP's
Currently, I have a database access class named DB , which uses PDO .
I have the following test-class made on the fly: http://sumoin.pastebin.com/ff744ea4 No fine-tuning or something
I have the following class and extension class (for this example): public class Person<T>
Recently, I took it upon myself to try and learn OO programming. It has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.