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

Related Questions

For example if i use $id= $_GET['id']; and then i use that $id as
There are several Databases that PDO can connect to but they all require a
I have a database class that uses PDO. Here's a portion example of it:
I am attempting to try to do something useful with PDO exceptions other than
For more information see this Example use strict; use warnings; use CGI::Simple; use DBI;
By dynamic data rating I mean a time-based recommendation system. One example use case
for example i use following command to find a record SELECT `users`.`mail` FROM `users`
I´m trying to change a spring jsp example to use freemarker. I changed all
I'll use an example to explain what behaviour I would like to achieve. If
Best to use an example to describe the problem. Lets say I have a

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.