我正在重构一些代码以使用guava Cache。
初始代码:
public Post getPost(Integer key) throws SQLException, IOException {
return PostsDB.findPostByID(key);
}
为了不破坏某些东西,我需要保留任何抛出的异常,而不是包装它。
当前的解决方案看起来有些丑陋:
public Post getPost(final Integer key) throws SQLException, IOException {
try {
return cache.get(key, new Callable<Post>() {
@Override
public Post call() throws Exception {
return PostsDB.findPostByID(key);
}
});
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof SQLException) {
throw (SQLException) cause;
} else if (cause instanceof IOException) {
throw (IOException) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause instanceof Error) {
throw (Error) cause;
} else {
throw new IllegalStateException(e);
}
}
}
有没有可能让它变得更好?
发布于 2011-12-27 23:00:29
就在写完这个问题之后,我开始思考泛型驱动的实用方法。然后想起了一些关于Throwables的事情。是的,它已经在那里了!)
可能还需要处理UncheckedExecutionException or even ExecutionError。
因此,解决方案是:
public Post getPost(final Integer key) throws SQLException, IOException {
try {
return cache.get(key, new Callable<Post>() {
@Override
public Post call() throws Exception {
return PostsDB.findPostByID(key);
}
});
} catch (ExecutionException e) {
Throwables.propagateIfPossible(
e.getCause(), SQLException.class, IOException.class);
throw new IllegalStateException(e);
} catch (UncheckedExecutionException e) {
Throwables.throwIfUnchecked(e.getCause());
throw new IllegalStateException(e);
}
}
非常好!
另请参阅ThrowablesExplained、LoadingCache.getUnchecked和Why we deprecated Throwables.propagate。
发布于 2021-05-25 18:50:15
只需使用龙目岛的@SneakyThrows
即可。强制异常包装不再有问题。
现在是2021年,Java仍然有检查过的异常...人们什么时候会明白,即使检查过的异常在理论上看起来很好,但它们在实践中制造了太多的问题?
长期解决方案:如果有机会,可以使用一种合适的语言,比如Kotlin。
https://stackoverflow.com/questions/8645965
复制相似问题