I am getting MongoConnectionException on trying to connect to mongodb server, when mongod is not running. Unfortunately, I am unable to catch the exception. Can someone tell me what is wrong with the following code?
try {
$conn = new \Mongo('localhost');
self::$mdb = $conn->filestore;
} catch(MongoConnectionException $e) {
trigger_error('Mongodb not available', E_USER_ERROR);
}
The above code fails with the Exception
Fatal error: Uncaught exception ‘MongoConnectionException’ with
message ‘Invalid argument’
I tried to catch the exception using MongoConnectionException and Exception classes, but both are not working.
Because you’re using \Mongo I suspect you’re in a namespace. Because of that, you need to catch on \MongoConnectionException (or \Exception).