2012-12-10 76 views
0

的Windows 7 64 SP1 的MongoDB 2.2.0 C++司机 MSVS 2010如何使用UserException处理的数据类型错误MongoDB中与C++司机

据:

http://api.mongodb.org/cplusplus/2.2.0/classmongo_1_1_b_s_o_n_element.html#a692f3eecb505eae2181eb938b7680fbe

Double()(和类似的功能)应该“抛出UserException如果元素不是所需的类型。”

我的代码:

BSONObj a_document = BSON("a_string"<<"A string"); 

try 
{ 
    a_document["a_string"].Double(); 
} 
catch(mongo::UserException ue) 
{ 
    cout << ue.toString() << endl; 
} 

但它不被逮住。 Intead它断言:

Sun Dec 09 16:04:28 Assertion: 13111:wrong type for field (a_string) 2 != 1 
Sun Dec 09 16:04:28 dev: lastError==0 won't report:wrong type for field (a_string) 2 != 1 

我做错了什么?我想自己去捕捉和处理数据类型异常。

谢谢!

回答

1

我从通过cocumentation和头看的感觉是,文件是不准确的,在这一点上,或使用一些选项,从MongoDB中禁用例外。

试试下面的代码:

BSONObj a_document = BSON("a_string"<<"A string"); 

try 
{ 
    a_document["a_string"].Double(); 
} 
catch(mongo::UserException& ue) 
{ 
    cout << "UserException: " << ue.toString() << endl; 
} 
catch(mongo::MsgAssertionException& ex) 
{ 
    cout << "MsgAssertionException: " << ex.toString() << endl; 
} 
catch(mongo::DBException& ex) 
{ 
    cout << "DBException: " << ex.toString() << endl; 
} 
catch(std::exception& ex) 
{ 
    cout << "std::exception: " << ex.what() << endl; 
} 

,看看哪个异常被抛出其实(如果有的话)。

+0

它引发MsgAssertionException。我会制作一个jira文章来纠正文档或我的理解。谢谢! –