doushi8187 2014-12-22 00:37
浏览 42

尝试在另一个try catch块中捕获块

I have the following scenario:

In my .db.inc.php file I'm connecting to a database:

try {
    $dbh = new PDO('mysql:host=' . HOST . ';dbname=' . DATABASE , USER_DB, PASSWORD_DB);
}
catch (PDOException $e) {
    die('Error!: ' . $e->getMessage() . '<br/>');
}

Then I run some functions before I need the connection, but I know that the connection is established as I don't have an error thrown.

Now I need to update my database:

include('.db.inc.php');
try {

/...
$sql = "UPDATE de_request_products SET report_id = :report_id WHERE request_id = :request_id";
$stmt = $dbh->prepare($sql);
$stmt->bindValue(':report_id', $report_number, PDO::PARAM_STR);
$stmt->bindValue(':request_id', $report_request_number, PDO::PARAM_STR);
$stmt->execute();
}
catch (PDOException $e){
die('Error!: ' . $e->getMessage() . '<br/>');
}

If above written try block fails, I want to have a try catch block in the Exception Handler to update the database:

// ....
catch (PDOException $e){
  try {

   //SEND EMAIL AND

     $sql = "INSERT INTO de_failed_requests (datum, reason) VALUES (:datum, :reason)";
     $stmt = $dbh->prepare($sql);
     $stmt->bindValue(':datum', $date, PDO::PARAM_STR);
     $stmt->bindValue(':reason', $e->getMessage(), PDO::PARAM_STR);
     $stmt->execute();
   } catch (PDOException $f) {
     die('NOTHING WORKS AT ALL!'. $f->getMessage());
     }
     die('Error!: ' . $e->getMessage() . '<br/>');
 }

Would this be the way to go or is there a more efficient way?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?