Error Handling:
Error handling using the .NET driver is a requirement for a fault-tolerant system. While MongoDB supports automatic replica set failover and the driver supports multiple mongos’s, this doesn’t make a program immune to errors.

Almost all errors will occur while attempting to perform an operation on the server. In other words, you will not receive an error when constructing a MongoClient, getting a database, or getting a collection.
This is because we are connecting to servers in the background and continually trying to reconnect when a problem occurs. For more additional info MongoDB Online Training
Only when you attempt to perform an operation do those errors become apparent.
Server Selection Errors:
Even when some servers are available, it might not be possible to satisfy a request. For example, using tag sets in a read preference when no server exists with those tags or attempting to write to a replica set when the Primary is unavailable.
Both of these would result in a TimeoutException. Below is an example exception (formatted for readability) when attempting to insert into a replica set without a primary.
System.TimeoutException: A timeout occured after 30000ms selecting a server using
CompositeServerSelector{
Selectors =
WritableServerSelector,
LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 }
}.
Client view of cluster state is
{
ClusterId : "1",
Type : "ReplicaSet",
State : "Connected",
Servers : [
{
ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/clover:30000" }",
EndPoint: "Unspecified/clover:30000",
State: "Disconnected",
Type: "Unknown"
},
{
ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/clover:30001" }",
EndPoint: "Unspecified/clover:30001",
State: "Connected",
Type: "ReplicaSetSecondary",
WireVersionRange: "[0, 3]"
},
{
ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/clover:30002" }",
EndPoint: "Unspecified/clover:30002",
State: "Disconnected",
Type: "Unknown"
},
]
}
Inspecting this error will tell you that we were attempting to find a writable server. The servers that are known are clover:30000, clover:30001, and clover:30002. Of these, clover:30000 and clover:30002 are disconnected and clover:30001 is connected and a secondary. Learn more skills from MongoDB Certification
By default, we wait for 30 seconds to fulfill the request. During this time, we are trying to connect to clover:30000 and clover:30002 in hopes that one of them becomes available and and takes the role of primary. In this case, we timed out waiting.
Connection Errors:
A server may be unavailable for a variety of reasons. Each of these reasons will manifest themselves as a TimeoutException. This is because, over time, it is entirely possible for the state of the servers to change in such a way that a matching server becomes available.
Connection Errors:
A server may go down after it was selected, but before the operation was executed. These will always manifest as a MongoConnectionException. Inspecting the inner exception will provide the actual details of the error.
MongoDB.Driver.MongoConnectionException: An exception occurred while receiving a message from the server.
---> System.IO.IOException: Unable to read data from the transport connection: Anestablished connection was aborted by the software in your host machine.
---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state)
at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
--- End of inner exception stack trace ---
... snip ...
We see from this exception that a transport connection that was successfully open at one point has been aborted.
There are too many forms of this type of exception to enumerate. In general, it is not safe to retry operations that threw a MongoConnectionException unless the operation was idempotent. Get practical skills from MongoDB Online Course
Simply getting an exception of this type doesn’t give any insight into whether the operations was received by the server or what happened in the server if it was received.
Write Exceptions:
When performing a write, it is possible to receive a MongoWriteException. This exception has two important properties, WriteError and WriteConcernError.
Write Error:
A write error means that there was an error applying the write. The cause could be many different things. The WriteError contains a number of properties which may help in the diagnosis of the problem. The Code property will indicate specifically what went wrong. For general categories of errors, the driver also provides a helpful Category property which classifies certain codes.
MongoDB.Driver.MongoWriteException: A write operation resulted in an error. E11000 duplicate key error index: test.people.$_id_ dup key: { : 0 }
---> MonoDB.Driver.MongoBulkWriteException`1[MongoDB.Bson.BsonDocument]: A bulk write oeration resulted in one or more errors. E11000 duplicate key error index: test.people.$_id_ dup key: { : 0 }
at MongoDB.Driver.MongoCollectionImpl`1.<BulkWriteAsync>d__11.MoveNext() in :\projects\mongo-csharp-driver\src\MongoDB.Driver\MongoCollectionImpl.cs:line 16
We see from this exception that we’ve attempted to insert a document with a duplicate _id field. In this case, the write error would contain the category DuplicateKeyError.
Write Concern Error:
A write concern error indicates that the server was unable to guarantee the write operation to the level specified. See the server’s documentation for more information.
Bulk Write Exceptions:
A MongoBulkWriteException will occur when using the InsertManyAsync or BulkWriteAsync. This exception is just a rollup of a bunch of individual write errors. It also includes a write concern error and a Result property.
To get in-depth knowledge, enroll for a live free demo on MongoDB Training