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 7498891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:37:50+00:00 2026-05-29T19:37:50+00:00

What is a decent way to handle PDO error when using try catch block?

  • 0

What is a decent way to handle PDO error when using try catch block?

Currently I have something like this:

BlogModel.php

   class BlogModel extends Model {
       public function save($id, $value) {
         $stmt = $this->getDb()->prepare('UPDATE setting SET name = :name WHERE id = :id');
         $stmt->bindParam(':id', $id);
         $stmt->bindParam(':name', $values);
         return ($stmt->execute() !== false) ? $id : false;
       }
   }

So, in the controller BlogController.php, I would do something like this:

<?php
class Blog extends Controller {

    public function comments()
    {
        $data = array();
        $model = new BlogModel;

        if ($model->save(2,'test')) {
                $data['result']['message'] = 'Settings saved';
                $data['result']['status'] = 'success';
        } else {
                $data['result']['message'] = 'Could not save the settings';
                $data['result']['status'] = 'error';
        }

        $view = new View("view.php", $data)
        $view->render();
    }
}
?>

This is the way I handle PDO error using if conditions. What is the decent way to translate this into try catch block? I don’t want to code the variables ($data['result']['message'] $data['result']['status']) all the time.

Is possible to add “throw exception” in the catch block somehow?

If there is a lot of try catch blocks in the controller, it going to look messy.. right?

  • 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-29T19:37:51+00:00Added an answer on May 29, 2026 at 7:37 pm

    None of the answers here are wrong. But actually all three combined are the real answer.
    You should definitely set

    $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    as said by Cerad.

    From now on every single issue about anything regarding database is thrown via exception of type PDOException. You just don’t have to throw your own Exception as said by ladar because it’s useless. Just take the ladar code and convert it into

     ...
     $data = array();
     $model = new BlogModel;
    
        try{
          $model->save(2,'test');
          $data['result']['message'] = 'Settings saved';
          $data['result']['status'] = 'success';
        }catch(PDOException $e){
            $data['result']['message'] = 'Could not save the settings';
            $data['result']['status'] = 'error';
        }
    

    And do NOT throw anything by yourself.

    Then a very nice way for debugging PDO queries is using the catch script linked by Basic that you can find here once again.

    Combining this things togheter you’ll have a flexible, clean and easy-debug way to catch all the errors that could come.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does windows have any decent sampling (eg. non-instrumenting) profilers available? Preferably something akin to
I'm working with pysvn, and I'm trying to find a decent way to handle
I've been fighting and fighting for some time with a decent way to handle
I'm currently using XmlSerializer to, surprisingly enough :), handle de/serialization of my data structures
Is there a decent way with static HTML/XHTML to create common header/footer files to
I was wondering if there was any decent way, other than NSLog-ing just about
I've spent a decent amount of time trying to hunt down a simple way
Anyone have a decent example, preferably practical/useful, they could post demonstrating the concept?
Does anyone have a decent algorithm for calculating axis minima and maxima? When creating
In a simple inventory management app i have an activity to handle assigning equipment

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.