I am doing a really basic insert:
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 }
Say for some reason that _databaseName is incorrect, so the driver cannot connect to the database. The paste operation does not work, obviously, but there are 3 things:
- He never throws a MongoException
- The only exception that I can catch in my catch block is the java null pointer exception
- mongoDb and collection objects are created, not null
However, in my Eclipse console, I can see more detailed exception messages like:
java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect
Is there any way to catch this exception?
thanks
Edit
In NullPointerException, unfortunately, there is no stacktrace, but only "java.lang.NullPointerException". However, here is what I see in the console before throwing a NullPointerException:
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)
This is what I want to catch, but there seems to be no way to do this, unfortunately ...
source share