【发布时间】:2012-01-09 23:57:45
【问题描述】:
这是我的代码-
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;
}
如果无法访问数据库,我希望 MongoDB 会抛出异常。但是,我发现if (!conn) 越来越满意了。
为什么
catch (DBException &e) {
cout << "caught " << err_msg << endl;
return 1;
}
块不起作用?
【问题讨论】:
标签: c++ exception mongodb exception-handling try-catch