I can enable PDO to throw error as exceptions to PDOException class.
Can I make it to throw it to my exception class?
Right now I have to code like this:
try {
$sql = "query";
$zap = $pdo->query($sql);
$sql_d=array();
if ($zap->execute($sql_d)) {
}
} catch (\PDOException $e) {
throw new dbException($zap->errorInfo(), $sql);
}
I want to PDO throw error to dbException class - not PDOException.
Related
From below code I expect that $query1 will be undone after Exception is
thrown. But it's not.
try {
$this->em->getConnection()->beginTransaction();
PDO::exec($query1 = 'INSERT INTO foo...')
throw new Exception();
PDO::exec($query2 = 'INSERT INTO bar...')
$this->em->flush();
$this->em->getConnection()->commit();
} catch (\Exception $e) {
$this->em->getConnection()->rollBack();
throw $e;
}
I use Yii and recently started using Transactions with try / catch blocks.
Here's how the code looks right now:
$dbConnection = Yii::app()->db();
try {
$transaction = $dbConnection->beginTransaction();
$dbConnection->createCommand("SELECT * from table_1")
->queryAll();
$transaction->commit();
} catch (Exception $ex) {
$transaction->rollback();
}
Suppose there's an exception with the DB (it's come up while unit-testing), I'm unable to rollback because the PHP dies with a fatal $transaction undefined error.
I'd rather not include isset() checks everywhere..
Is there a simpler way to make this work?
You can check if the exception is an instance of CDbException
$dbConnection = Yii::app()->db();
try {
$transaction = $dbConnection->beginTransaction();
$dbConnection->createCommand("SELECT * from table_1")
->queryAll();
$transaction->commit();
} catch (Exception $ex) {
if ($ex instanceof CDbException)
{
// handle CDBException
// ...
}
$transaction->rollback();
}
Is there a way to setup a Pdo object to throw a custom exception instead of the default PDOException?
For example:
class MyCustomDbException extends PDOException{}
$pdo = new Pdo("mysql:host=localhost;dbname=myapp", "user_name", "secret_password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EXCEPTION_CLASS, "MyCustomDbException");
try {
// Code is here
} catch (PDOException $e) {
// See exception manual if you want to path through message or anything else from pdo exception.
throw new YourException('PDO exception was thrown');
}
http://php.net/manual/en/language.exceptions.extending.php
to see how you can path through parameters.
I have the following script.
According to http://php.net/manual/en/class.pdoexception.php, You should not throw a PDOException from your own code.
But I want the same catch to be performed whether a PDOException or the exception that I threw for an invalid foo.
I've also been told that I should never catch the generic Exception, but only catch specific Exceptions.
How should this be implemented?
try {
db::db()->beginTransaction();
//Do a bunch of queries, and a PDO exception will be thrown upon error
if($foo($bar)) {throw new Exception('Invalid foo.');}
db::db()->commit();
} catch (PDOException $e) {
db::db()->rollBack();
//Maybe do some other stuff
}
Something like
try {
db::db()->beginTransaction();
//Do a bunch of queries, and a PDO exception will be thrown upon error
if($foo($bar)) {throw new RuntimeException('Invalid foo.');}
db::db()->commit();
} catch (PDOException $e) {
db::db()->rollBack();
//Maybe do some other stuff
} catch (RuntimeException $e) {
//foo invalid
}
I'm building a custom exception class to manage all exceptions:
class MyExceptions extends Exception
{
public function __construct($message = 'Unkown errror', $code = -1, Exception $previous = null) {
echo 'init!';
parent::__construct($message, $code, $previous);
}
}
Now, when a PDOException occurs, I want to re-throw it to MyExceptions:
class myDB
{
private $db;
public function dbConnect() {
try {
$this->db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');
}
catch (PDOException $e) {
throw new MyExceptions($e);
}
/* Updated */
catch (MyExceptions $e) {
echo 'caught!';
}
}
}
The problem is that when a db connection exception rises, I get the following fatal error on screen:
init!
Fatal error: Uncaught exception 'MyExceptions' with message...
So, the exception is not caught, although the MyExceptions __construct() is called (see the 'init!' displayed).
Every bit of resource I read points to the exact implementation as mine, I have no idea what I'm doing wrong.
You need
try {
try {
$this->db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');
} catch (PDOException $e) {
throw new MyExceptions($e);
}
} catch (MyExceptions $f) {
echo 'caught!';
}
Sequential catch blocks are for different types of exceptions thrown within the try.
You are throwing it:
throw new MyExceptions($e);
^^^^^
And then you don't catch it. So what do you wonder about?
Also you should add the previous exception at third position (for previous) instead of the first position (for message).
It doesn't go through all the catch blocks. Only the first one that matches. If you then throw another exception inside a catch block, you'd have to catch it in another try block around the first one.