doucong1268 2018-04-09 07:17
浏览 277

php:捕获PDO异常并继续执行?

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 );
        }
  • 写回答

2条回答 默认 最新

  • dongmeijian1716 2018-04-09 07:23
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题