Here is my code-
DBClientBase *conn = NULL;
string err_msg;
ConnectionString cs = ConnectionString::parse(connString, err_msg);
if (!cs.isValid()) {
throw "bad: " + err_msg;
}
try {
conn = cs.connect(err_msg);
}
catch (DBException &e) {
cout << "caught " << err_msg << endl;
return 1;
}
if (!conn) {
cout << "Unable to connect to DB" << endl;
return 1;
}
I would expect MongoDB to throw exception in case DB is not reachable. However, I am finding that if (!conn) is getting satisfied.
Why
catch (DBException &e) {
cout << "caught " << err_msg << endl;
return 1;
}
block isn’t working?
From the current trunk source,
ConnectionString::connectonly seems to throw an exception when the string itself was invalid (and you already know that it was not, from your first conditional statement).It just returns a NULL pointer and sets
errMsgin all other cases.In your defence, I couldn’t find this documented anywhere at all; a very basic example of
connectwas all I could locate.