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

时间:2016-04-26 11:11:10

标签: php mongodb exception-handling try-catch

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

<?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 CLI上运行。在PHP CLI中,我们在控制台上获得了所有异常,但出于教学目的,我想在try / catch块中捕获异常。

我看到了比PHP更多的Java代码,因此,捕获泛型MongoDB\Driver\Exception\Exception对我来说并不好看。在Java中,我们捕获特定的异常,并为不同类型的异常提供多个try / catch块。

驱动程序具有以下例外:

MongoDB\Driver\Exception\AuthenticationException
MongoDB\Driver\Exception\BulkWriteException 
MongoDB\Driver\Exception\ConnectionException 
MongoDB\Driver\Exception\ConnectionTimeoutException 
MongoDB\Driver\Exception\Exception 
MongoDB\Driver\Exception\ExecutionTimeoutException 
MongoDB\Driver\Exception\InvalidArgumentException 
MongoDB\Driver\Exception\LogicException 
MongoDB\Driver\Exception\RuntimeException 
MongoDB\Driver\Exception\SSLConnectionException 
MongoDB\Driver\Exception\UnexpectedValueException 
MongoDB\Driver\Exception\WriteException

这是一种在PHP中捕获异常的犹太方法吗?

3 个答案:

答案 0 :(得分:3)

您可以添加多个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";
}

?>

答案 1 :(得分:2)

如何在catch部分中放置switch语句,并使用instanceof语言结构或get_class()函数确定异常的类型?

例如:

[...]

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

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

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

答案 2 :(得分:0)

添加多个catch语句我认为这是比switch更好的方法。但是,如果异常不是来自这些类的开关,那您应该有一个默认的大小写,并且如果您使用多个catch来处理它。

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";
}