我做這樣一個非常基本的插入:的MongoDB的Java驅動程序:捕捉異常,當插入失敗
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
}
比方說,不管什麼原因,_databaseName是不正確的,所以驅動程序無法連接到數據庫。插入操作失敗,很明顯,但有三樣東西:
- 它不會拋出一個MongoException
- 我可以在我的「抓」塊趕上唯一的例外是一個通用的Java空指針異常「
- 的MongoDB和創建集合對象,而不是空
然而,在我的Eclipse控制檯,我可以清楚地看到這樣更詳細的異常信息:
java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect
有沒有辦法來捕捉這個異常?
感謝
編輯
的NullPointerException異常不包含堆棧跟蹤遺憾的是,只有微薄的 「顯示java.lang.NullPointerException」。然而,這裏是我在控制檯中看到的,則拋出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)
這就是我要趕什麼,但似乎沒有辦法做到這一點很遺憾......
你試過顯式地捕獲一個'IOException'嗎? – mre
是的,我嘗試過,但它永遠不會拋出...... – Mathieu
在抓到'Exception'之前'catch''IOException'嗎? – mre