0

Windows 7 64 SP1 MongoDB 2.2.0 C++ ドライバー MSVS 2010

によると:

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;
}

でも引っかからない。代わりに、次のように主張します。

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

私は何を間違っていますか?データ型の例外を自分でキャッチして処理したい。

ありがとう!

4

1 に答える 1

1

コキュメンテーションとヘッダーを見て感じたのは、この時点でドキュメントが不正確であるか、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;
}

実際にスローされる例外を確認します (存在する場合)。

于 2012-12-10T18:27:38.303 に答える