PHP: Can iterator_to_array () throw an exception on MongoCursor

Can use iterator_to_array()in an instance MongoCursorthrow an exception in PHP 5.3? In other words, do I need to wrap calls iterator_to_array()in instances MongoCursorin try-catch statements or not?

eg.

$mongo = new Mongo();
$mongo_db = $mongo['my_database'];
$mongo_coll = $mongo_db['my_collection'];

// This

$cursor = $mongo_coll->find();
$documents = iterator_to_array($cursor);

// Versus this.

$cursor = $mongo_coll->find();
try {
    $documents = iterator_to_array($cursor);
} catch (Exception $e) {
    //...
}
+3
source share
3 answers

iterator_to_array()may throw exceptions because it calls next () .

+1
source

I think the first comment so far on this page http://www.php.net/manual/en/mongo.queries.php will be of interest to you, but I don’t know, it will be the first when you look at it, so here's the deal.

, , $cursor->valid(). , , , , - .

...
$cursor = $mongo_coll->find();
$cursor->rewind();
if ($cursor->valid()) {
    $documents = iterator_to_array($cursor);
}

catch catch , catch try , , .

0

The find method returns a Traversable object or throws an exception.

Iterator_to_array accepts a Traversable object.

An exception should only appear if something really bad happens in the Mongo driver or Mongo during the iteration. Maybe a disconnect.

0
source

All Articles