(PECL mongo >= 1.5.0)
Examples:
Catching MongoDuplicateKeyException
1
2
3
4
5
6
7
8
9
10
11
12
<?php
$mc new MongoClient("localhost");
 
$c $mc->selectCollection("test""test");
 
$c->insert(array('_id' => 1));
try {
    $c->insert(array('_id' => 1));
catch (MongoWriteConcernException $e) {
    echo $e->getMessage(), "\n";
}
?>

The above examples will output something similar to:

localhost:27017: insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.test.$_id_  dup key: { : 1 }