Hafeez Khan
Hafeez Khan

Reputation: 487

Mongodb c# InsertOne() - How to Handle exception

I am using mongodbc# driver which includes legacy driver. And using InsertOne() method insert records. But it is void, how do I know if there is error in insertion and what type of exception is thrown.

Upvotes: 1

Views: 5785

Answers (2)

Ajas Aju
Ajas Aju

Reputation: 745

Just use Try Catch

try {
db.products.insertOne(
   { "item": "envelopes", "qty": 100, type: "Self-Sealing" },
   { writeConcern: { w : "majority", wtimeout : 100 } }
);
} catch (e) {
   print (e);
}

it will catch the Exception.

Upvotes: 0

Craig Wilson
Craig Wilson

Reputation: 12624

It will raise an exception if an error occurs. http://mongodb.github.io/mongo-csharp-driver/2.2/reference/driver/error_handling/#write-exceptions

Upvotes: 1

Related Questions