4

I'm doing a really basic insert like this:

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
}

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:

  • It never throws a MongoException
  • The only exception I can catch in my 'catch' block is a generic 'java null pointer exception'
  • mongoDb and collection objects are created and not null

However, in my Eclipse console, I can clearly see more verbose exception messages like:

java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect

Is there a way to catch this exception?

Thanks

Edit

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:

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)

That's what I want to catch, but there seems to be no way to do so unfortunately...

8
  • Have you tried to explicitly catch an IOException? Commented Aug 3, 2011 at 12:31
  • Yes I did try that but it is never thrown... Commented Aug 3, 2011 at 12:34
  • Did you catch the IOException before you caught the Exception? Commented Aug 3, 2011 at 12:35
  • In fact, I can't even use a catch(IOException ex) block. Eclipse gives me an 'Unreachable catch block for IOException' error. Commented Aug 3, 2011 at 12:38
  • That's because the catch block for the IOException needs to precede the catch block for the Exception block. If that doesn't work, then clearly the method will not throw an IOException. Commented Aug 3, 2011 at 12:39

2 Answers 2

2

I was able to reproduce the behaviour and in fact you will only be able to catch a NullpointerException when you try to insert an object into an unreachable MongoDB instance. IMHO this behaviour should be fixed in the MongoDB Java Driver, as it is not very Java-ish. The dirty workaround looks probably something like this:

private static void safeInsert(DBCollection c, DBObject o) {
    if (c == null) {
        throw new RuntimeException("collection must not be null");
    }

    if (o == null) {
        throw new RuntimeException("object must not be null");
    }

    try {
        c.insert(o);
    } catch (NullPointerException e) {
        throw new RuntimeException("unable to connect to MongoDB " + c.getFullName(), e);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I think I wont have any other choice but to do something like that... Thanks for answering!
0

put the

DB mongoDb = _mongo.getDB(_databaseName);
DBCollection collection = mongoDb.getCollection(_collectionName);

in the try block too.

1 Comment

Yeah, I did try that, but no luck :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.