有一些频率,我发现自己编写的API提供了Iterator<Foo>
,它由网络连接支持。该实现打开网络连接,从流中读取信息,并将该信息反序列化为Foo
以传递给调用者。不幸的是总是存在IOException
的可能性,并且还需要优雅地关闭网络连接(当调用者读取最后一个Foo
时可以自动关闭,但如果没有发生怎么办? )。
关于如何处理将在Iterator
的实现中抛出的已检查异常,已经有一些问题(here和here),并且接受的建议是“将它们包装在未经检查的RuntimeException
s中。同时为了允许关闭网络连接,我们可以实现Closeable
。因此,对于行为良好的异常检查调用者,我们最终得到类似的东西:
Iterator<Foo> iter = null;
try {
iter = getFooIterator();
while(iter.hasNext()) {
Foo foo = iter.next();
// do something with foo
}
}
catch(RuntimeException e) {
if(e.getCause() instanceof IOException) {
// do something with the IOException
}
else throw e;
}
finally {
if(iter instanceof Closeable) try { ((Closeable)iter).close(); } catch(IOException e) {}
}
实施Iterator
似乎是个不错的主意。还有更好的方法吗?
答案 0 :(得分:1)
IMO的第一步是将其包含在实现或特定于应用程序的异常中,从而无需捕获泛型RuntimeException
或检查根本原因。
我会考虑一个特定的实现来避免关闭检查并结束IOException
。
NetworkIterator<Foo> iter = null;
try {
iter = getFooIterator();
while (iter.hasNext()) {
Foo foo = iter.next();
// do something with foo
}
} catch (NetworkIteratorExceptiom e) {
// do something with the IOException
} finally {
iter.close();
}
我可能不会给它方法让模板消失,但我会受到诱惑;大致是:
NetworkIterator<Foo> iter = new IteratorThang<Foo>() {
@Override public void each(Foo foo) {
// Do something with foo
}
};