title: MongoDB handling failure of insert in Java
body: <p>I would like to handle failure of insert to collection (using Java) in order to be sure that my insert was successful. In case that insert fails, I want to perform some fall-back action.</p>
  
  <p>Assuming following code in Java and latest mongo driver (version 2.11.3):</p>
  
  <pre><code>BasicDBObject doc = new BasicDBObject("name", "MongoDB");
  WriteResult result = coll.insert(WriteConcern.SAFE, doc);
  </code></pre>
  
  <p>I am confused by the fact that the insert returns WriteResult and it could throw MongoException. What am I supposed to do in order to safely detect any possible failure of insert? Please, provide code example. And if you can clarify when insert throws exception and when it just returns some error write result. I tried to search in java driver API docs at <a href="http://api.mongodb.org/java/2.11.3/" rel="nofollow">http://api.mongodb.org/java/2.11.3</a>/ for it; howerever, this infromation is missing there.</p>
  
  <pre><code>WriteResult result;
  try {
      result = coll.insert(WriteConcern.SAFE, doc);
  }
  catch(MongoException ex){
      logger.warn("Insert failed.", ex);
      throw ex;
  }
  
  //Shall I check result here for additional errors?  
  </code></pre>
  
  <p>If I should check, what type of error I am able to detect by checking insert result?</p>