0

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 );
        }
4
  • Are you within a namespace? Try \PDOException within the catch. Commented Apr 9, 2018 at 7:19
  • No, its not within a namespace.. Commented Apr 9, 2018 at 7:29
  • how about cache the generic exception? catch (Exception $e) { // Do stuff } Commented Apr 9, 2018 at 7:57
  • Tried generic Exception handling also but strangely its also not working... Commented Apr 9, 2018 at 8:15

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I tried but even this also not working ... catch (DbEX $exx) { //throw new Exception('Unable to connect'); }
0

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.

2 Comments

May I see your custom Exception code? "DbEX" and what is the error message?
If you could also add you class DbEx code on your question it will help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.