我正在做一个非常基本的插入,如下所示:
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 不正确,因此驱动程序无法连接到数据库。插入操作显然失败了,但有 3 件事:
- 它永远不会抛出 MongoException
- 我可以在“catch”块中捕获的唯一异常是通用“java 空指针异常”
- mongoDb 和集合对象已创建且不为 null
然而,在我的 Eclipse 控制台中,我可以清楚地看到更详细的异常消息,例如:
java.io.IOException: couldn't connect to [/127.0.0.1:27017] bc:java.net.ConnectException: Connection refused: connect
有没有办法捕获这个异常?
Thanks
Edit
不幸的是,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)
这就是我想要捕捉的,但不幸的是似乎没有办法做到这一点......
我能够重现该行为,事实上,当您尝试将对象插入无法访问的 MongoDB 实例时,您只能捕获 NullpointerException。恕我直言,这种行为应该在 MongoDB Java 驱动程序中修复,因为它不太像 Java 风格。肮脏的解决方法可能看起来像这样:
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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)