Catching the exception is the only way, but you can make it easier .
PHP Code:
class LocalDB extends PDO {
static $dbs = array();
static function connect($db='dflt') {
if (! isset(self::$dbs[$db])) {
try {
self::$dbs[$db] = new PDO("mysql:hostname=localhost;dbname=$db", 'user', 'passwd');
} catch (PDOException $exc) {
// erase frame w/ password from call trace.
throw new PDOException($exc->getMessage(), $exc->getCode(), $exc->getPrevious());
}
self::$dbs[$db]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$dbs[$db];
}
}
RAII support left as an exercise.