我正在使用spymemcached-2.8.4.jar和jdk.1.7。
这是我的代码
try {
MemcachedClient client = new MemcachedClient(...);
client.set('name', 1000, 'some_name');
}catch(Exception ex){
System.out.println("Exception occurred");
System.out.println(ex.getMessage());
ex.printStackTrace();
logExceptionInDB(ex);
}
在我的scansion中,我的memcached机器没有运行,所以它在控制台中打印以下异常,
java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:735)
at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:369)
at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:242)
at net.spy.memcached.MemcachedConnection.run(MemcachedConnection.java:836)
但我想抓住这个,除了把它写在数据库中。如何捕获此异常?
答案 0 :(得分:1)
您无法捕获帖子中提到的异常,因为它已被spymemcached本身捕获,您只能看到日志,MemcachedConnection.java
中的相关代码是:
catch (ConnectException var5) {
this.getLogger().info("Reconnecting due to failure to connect to %s", new Object[]{node, var5});
this.queueReconnect(node);
}
之后会尝试重新连接,因此请确保您的配置正确
实际上,您可以捕获set
方法产生的异常:
OperationFuture<Boolean> future = client.set("name", 1000, "some_name");
Boolean result = future.get(50, TimeUnit.MILLISECONDS);
假设一次操作的超时时间为50毫秒,经过一段时间后,您将获得CheckedOperationTimeoutException
。