【发布时间】:2012-10-10 12:09:07
【问题描述】:
在 Guava 中使用 Throwables.propagate(e) 时处理 InterruptedExceptions 的最佳做法是什么?
我喜欢使用throw Throwables.propagate(e),尤其是在不引发检查异常以及异常处理由调用者负责的方法中。但它并没有达到我对 InterruptedException 的期望。
我不想失去线程被中断的事实,所以我最终写了这样的东西:
public void run() {
Callable c = ...;
try {
c.call();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw Throwables.propagate(e);
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
有没有办法在 Guava 中做到这一点?是否有(向后兼容?!)使用类似 Throwables.propagate() 的方法将线程设置为中断,如果它正在包装和传播 InterruptedException?
【问题讨论】:
-
我会将此作为 Guava 中的功能请求提出
标签: java exception-handling guava interrupted-exception