如何在PHP和MongoDB中正确捕获异常

如何在PHP和MongoDB中正确捕获异常,php,mongodb,exception-handling,try-catch,Php,Mongodb,Exception Handling,Try Catch,我的问题是关于在PHP中捕获异常的正确方法。 在PHP MongoDB驱动程序的基础上,我 已创建以下脚本: <?php try { $mng = new MongoDB\Driver\Manager("mongodb://localhost:2717"); $query = new MongoDB\Driver\Query([], ['sort' => [ 'name' => 1], 'limit' => 5]); $rows =

我的问题是关于在PHP中捕获异常的正确方法。 在PHP MongoDB驱动程序的基础上,我 已创建以下脚本:

<?php

try {

    $mng = new MongoDB\Driver\Manager("mongodb://localhost:2717");
    $query = new MongoDB\Driver\Query([], ['sort' => [ 'name' => 1], 'limit' => 5]);     

    $rows = $mng->executeQuery("testdb.cars", $query);

    foreach ($rows as $row) {

        echo "$row->name : $row->price\n";
    }

} catch (MongoDB\Driver\Exception\Exception $e) {

    $filename = basename(__FILE__);

    echo "The $filename script has experienced an error.\n"; 
    echo "It failed with the following exception:\n";

    echo "Exception:", $e->getMessage(), "\n";
    echo "In file:", $e->getFile(), "\n";
    echo "On line:", $e->getLine(), "\n";       
}

?>

这是在PHP中捕获异常的一种更合适的方法吗?

在捕获部分放置一个switch语句,并用语言构造或函数确定异常的类型如何

例如:

[...]

} catch(\Exception $e) {
   switch (get_class($e)) {
     case 'MongoDB\Driver\Exception\AuthenticationException':
       // do stuff
       break;

     case 'MongoDB\Driver\Exception\BulkWriteException':
     //etc, etc...
   }
}

首先,我将检查get_class()的返回值,以确保将结果与确切的异常名称进行比较。

您可以添加多个catch语句

<?php

try {

    $mng = new MongoDB\Driver\Manager("mongodb://localhost:2717");
    $query = new MongoDB\Driver\Query([], ['sort' => [ 'name' => 1], 'limit' => 5]);     

    $rows = $mng->executeQuery("testdb.cars", $query);

    foreach ($rows as $row) {

        echo "$row->name : $row->price\n";
    }

} catch (MongoDB\Driver\Exception\AuthenticationException $e) {

    echo "Exception:", $e->getMessage(), "\n";
} catch (MongoDB\Driver\Exception\ConnectionException $e) {

    echo "Exception:", $e->getMessage(), "\n";
} catch (MongoDB\Driver\Exception\ConnectionTimeoutException $e) {

    echo "Exception:", $e->getMessage(), "\n";
}

?>

添加多个catch语句我认为这是比switch更好的方法。但是,如果异常不是来自这些带有开关的类,那么您应该有一个默认情况,并且如果您使用多个捕获来执行此操作,该怎么办

try {

$mng = new MongoDB\Driver\Manager("mongodb://localhost:2717");
$query = new MongoDB\Driver\Query([], ['sort' => [ 'name' => 1], 'limit' => 5]);     

$rows = $mng->executeQuery("testdb.cars", $query);

foreach ($rows as $row) {

    echo "$row->name : $row->price\n";
}

} catch (MongoDB\Driver\Exception\AuthenticationException $e) {
    echo "AuthenticationException:", $e->getMessage(), "\n";

} catch (MongoDB\Driver\Exception\ConnectionException $e) {
    echo "ConnectionException:", $e->getMessage(), "\n";

} catch (MongoDB\Driver\Exception\ConnectionTimeoutException $e) {
    echo "ConnectionTimeoutException:", $e->getMessage(), "\n";

}catch (\Exception $e) {
       echo "Exception:", $e->getMessage(), "\n";
}

在Java中就是这样做的。问题是这样做是否正确。(我没有看到PHP中的代码是这样的。)而且
MongoDB\Driver\Exception\Exception
也捕获了
ConnectionTimeoutException
,所以这样做可能是多余的。是的,这是Java方式,在PHP中也是如此。有关更多信息,请参见。MongoDB\Driver\Exception\Exception是捕获所有异常的通用类,必须用作捕获语句中未特别捕获的异常的回退。我们可以这样做,但这是PHP中捕获异常的推荐方法吗?若然,原因为何?
try {

$mng = new MongoDB\Driver\Manager("mongodb://localhost:2717");
$query = new MongoDB\Driver\Query([], ['sort' => [ 'name' => 1], 'limit' => 5]);     

$rows = $mng->executeQuery("testdb.cars", $query);

foreach ($rows as $row) {

    echo "$row->name : $row->price\n";
}

} catch (MongoDB\Driver\Exception\AuthenticationException $e) {
    echo "AuthenticationException:", $e->getMessage(), "\n";

} catch (MongoDB\Driver\Exception\ConnectionException $e) {
    echo "ConnectionException:", $e->getMessage(), "\n";

} catch (MongoDB\Driver\Exception\ConnectionTimeoutException $e) {
    echo "ConnectionTimeoutException:", $e->getMessage(), "\n";

}catch (\Exception $e) {
       echo "Exception:", $e->getMessage(), "\n";
}