原代码类似这样:
try {
CountDownLatch latch = new CountDownLatch(n);
//...
latch.await();
} catch (InterruptedException e) {
log.error("...", e);
throw new MyAppException("...");
}
Sonar 的解释是,如果你手动捕获中断异常,就得要么重新抛出它,要么手动中断线程,并且在那之前完成必要的清理工作:
If an InterruptedException or a Threadeath error is not handled properly, the information that the thread was interrupted will be lost. Handling this exception means either to re-throw it or manually re-interrupt the current thread by calling Thread.interrupt(). Simply logging the exception is not sufficient and counts as ignoring it. Between the moment the exception is caught and handled, is the right time to perform cleanup operations on the method's state, if needed.
我却对此陷入了疑惑,首先我很少见到在业务代码里直接中断线程的,这应该不是最合适的处理方式。其次,就算一直向上传递InterruptedException,到DispatcherServlet也当成一般异常处理了,那感觉和抛自定义异常是一样的(简单跟了下代码得出的结论,可能不准确),所以我没有看出那样修改的必要。
我的想法是否正确呢?如果有误请纠正我,谢谢大佬们