PDO Can't catch exception which is thrown

exception, mysql, pdo, php

Solution

Have you been using namespaced classes in this project maybe? If so, please try to use

\PDOException

instead of

PDOException

in your "catch" statement. I just got similar problem and this move helped.

Problem

Ok so I can't catch the exeption: ``` require_once 'config/config.php'; try { $dbh = new PDO('mysql:host='.DB_HOST.';dbname='.DB_DATABASE, DB_USER, DB_PASS,array( PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )); } catch (PDOException $e) { print "Unable to connect!: " . $e->getMessage() . BR; die(); } try { $stmt = $dbh->prepare("SELECT id,email FROM no_table ORDER BY id DESC LIMIT 5"); $stmt->execute(); $stmt->bindColumn(1, $id, PDO::PARAM_INT); $stmt->bindColumn('email', $email, PDO::PARAM_STR); while ($row = $stmt->fetch(PDO::FETCH_BOUND)) { echo $id . "\t" . $email . BR; } } catch (PDOException $e) { echo "Failed: " . $e->getMessage(); } ``` Ok I connect I have an error in the syntax (there isn't any no_table) it throws the exception but i can't catch it. In browser I see this: ``` Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'no_table' doesn't exist' in 'somepath' PDOStatement->execute() #1 {main} thrown in some.php on line 15 ``` The exception is thrown and i can't catch it, but if I put another try catch between `$stmt->execute();` it catches it. My php version 5.3.14

Original source