EternalSunShine
EternalSunShine

Reputation: 409

php: catch PDO exception and continue execution?

My project using PDO exceptional handling in 'db.class.php' which is global file for all libraries which can't be changed due to impact.

In some point of time, application is trying to connect a new database and if new database is not accessible then script should ignore this error and continue the execution.

New DB connection exception is handled in 'db.class.php' and when I am trying to handle the exception while connecting a new database, somehow exception handling not working and script is stopped at that point.

If I am not handling exception while connecting to new DB, in this case also script stopped executing.

Requirement is even if DB is not connecting in this case due to any issue, script should continue the execution ignoring the error.

Code:

try {   
        $newDb = new DB(DB_HOST_new, DB_NAME_new, DB_USER_new, DB_PASS_new, DB_UTC_TIMEZONE);
        $isDbSsConnected = true ;
    } catch (PDOException $exx)  {          
        //throw new Exception('Unable to connect');
    }

db.class.php

try {
            $connection = new PDO( $dsn, $username, $password );
            $connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

            if ( $utc ) {
                $this->setUtc( $connection );
            }

            $this->connection = $connection;
            self::$connections[$dsn][$username] = self::$connectionCachingEnabled ? $this->connection : NULL;

        } catch ( PDOException $ex ) {
            throw new DbEx( "Initialize Failed: DSN = " . $dsn, DbEx::INIT_FAILED, $ex );
        }

Upvotes: 0

Views: 1548

Answers (2)

Aljay
Aljay

Reputation: 456

Assuming this is your custom exception name DbEx

/**
 * Define a custom exception class
 */
class DbEx extends Exception
{
}

Then you have DB class

class DB
{
    public function __construct($host, $name, $username, $password, $timezone)
    {
        $dsn = "mysql:host=$host;dbname=$name";
        $username = $username;
        $password = $password;
        $utc = $timezone;

        try {
            $connection = new PDO( $dsn, $username, $password );
            $connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        } catch ( PDOException $ex ) {
            throw new DbEx("Initialize Failed: DSN");
        }
    }
}

Now you have a page/file that you will call the connection from class DB

try {   
    $newDb = new DB('localhost', 'test', 'root', '', true);
    $isDbSsConnected = true ;
} catch (DbEx $exx)  {          
    // catch error from DB class
    echo "script should go here ignoring the error";
}

The code above will work as your requirements, however, with your code on this part

throw new DbEx( "Initialize Failed: DSN = " . $dsn, DbEx::INIT_FAILED, $ex );

I think you need to double check the error message the error might came from this code itself.

Upvotes: 0

Vince0789
Vince0789

Reputation: 361

The db.class.php file is throwing a DbEX exception which is probably the one you should be catching, not the generic PDOException.

Upvotes: 1

Related Questions