2016-04-26 19 views
1

私の質問は、PHPで例外をキャッチする正しい方法についてです。 PHP MongoDBのドライバの伴うexamplesに基づいて、私は次のスクリプト作成した :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 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で例外をキャッチするユダヤの方法は何ですか?

答えて

1

キャッチ部分に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(の戻り値を調べます。

+0

複数のcatchステートメントを追加することができますが、これはPHPでの例外をキャッチするための推奨方法は何ですか?もしそうなら、なぜですか? –

2

あなたは私たちがこのような方法でそれを行うことができます

<?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

これはJavaで行われる方法です。問題は、それが適切な方法であるかどうかです。 (私はそのようにPHPでコードを見ませんでした)。また、 'MongoDB \ Driver \ Exception \ Exception'も' ConnectionTimeoutException 'をキャッチします。 –

+0

はい、それはJavaの方法であり、PHPでも同じです。詳細はhttp://php.net/manual/en/language.exceptions.phpをご覧ください。 MongoDB \ Driver \ Exception \ Exceptionはすべての例外をキャッチする汎用クラスであり、catchステートメントで特に検出されなかった例外のフォールバックとして使用する必要があります。 – prabhat

関連する問題