Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoDB-Java driver: Catch exception when insert fails
    text
    copied!<p>I'm doing a really basic insert like this:</p> <pre><code>try { DB mongoDb = _mongo.getDB(_databaseName); DBCollection collection = mongoDb.getCollection(_collectionName); collection.insert(myBasicDBObject); } catch (IOException ex) { // Unreachable code } catch (MongoException ex) { // Exception never thrown } catch (Exception ex) { // Handle exception } </code></pre> <p>Let's say for whatever reason that the _databaseName is incorrect, so the driver can't connect to the database. The insert operation fails, obviously but there's 3 things:</p> <ul> <li>It never throws a MongoException</li> <li>The only exception I can catch in my 'catch' block is a generic 'java null pointer exception'</li> <li>mongoDb and collection objects are created and not null</li> </ul> <p>However, in my Eclipse console, I can clearly see more verbose exception messages like:</p> <pre><code>java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect </code></pre> <p>Is there a way to catch this exception?</p> <p>Thanks</p> <p><strong>Edit</strong></p> <p>The NullPointerException contains no stacktrace unfortunately, only a meagre "java.lang.NullPointerException". However, here is what I see in the console, before the NullPointerException is thrown:</p> <pre><code>2011-08-05 10:06:52 com.mongodb.DBTCPConnector fetchMaxBsonObjectSize ATTENTION: Exception determining maxBSON size using0 java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect at com.mongodb.DBPort._open(DBPort.java:206) at com.mongodb.DBPort.go(DBPort.java:94) at com.mongodb.DBPort.go(DBPort.java:75) at com.mongodb.DBPort.findOne(DBPort.java:129) at com.mongodb.DBPort.runCommand(DBPort.java:138) at com.mongodb.DBTCPConnector.fetchMaxBsonObjectSize(DBTCPConnector.java:419) at com.mongodb.Mongo.getMaxBsonObjectSize(Mongo.java:541) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:237) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:210) at com.mongodb.DBCollection.insert(DBCollection.java:80) at foo.App.main(App.java:25) 2011-08-05 10:06:53 com.mongodb.DBTCPConnector fetchMaxBsonObjectSize ATTENTION: Exception determining maxBSON size using0 java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect at com.mongodb.DBPort._open(DBPort.java:206) at com.mongodb.DBPort.go(DBPort.java:94) at com.mongodb.DBPort.go(DBPort.java:75) at com.mongodb.DBPort.findOne(DBPort.java:129) at com.mongodb.DBPort.runCommand(DBPort.java:138) at com.mongodb.DBTCPConnector.fetchMaxBsonObjectSize(DBTCPConnector.java:419) at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:406) at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:144) at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:137) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:255) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:210) at com.mongodb.DBCollection.insert(DBCollection.java:80) at foo.App.main(App.java:25) 2011-08-05 10:06:54 com.mongodb.DBPortPool gotError ATTENTION: emptying DBPortPool to 127.0.0.1:27017 b/c of error java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect at com.mongodb.DBPort._open(DBPort.java:206) at com.mongodb.DBPort.go(DBPort.java:94) at com.mongodb.DBPort.go(DBPort.java:75) at com.mongodb.DBPort.say(DBPort.java:70) at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:151) at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:137) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:255) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:210) at com.mongodb.DBCollection.insert(DBCollection.java:80) at foo.App.main(App.java:25) </code></pre> <p>That's what I want to catch, but there seems to be no way to do so unfortunately...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload