重新中断此方法或重新引发声纳中的“ InterruptedException问题”

时间:2019-01-30 07:03:54

标签: java exception try-catch throws interrupted-exception

在我的方法之一中,中断异常和执行异常即将到来。 我像这样放了一个尝试。

try{

  //my code
}catch(InterruptedException|ExecutionException e)

  Log.error(" logging it");
  throw new MonitoringException("it failed" , e)


//monitoringexception extends RunTimeException

在我的方法中,我也抛出了InterruptedException,ExecutionException

我的声纳信号严重不足-请重新中断此方法或重新抛出“ InterruptedException

任何人都知道如何解决此问题。

请立即提供帮助。

1 个答案:

答案 0 :(得分:15)

以“重新中断”为最佳做法:

try{
    //some code
} catch (InterruptedException ie) {
    logger.error("InterruptedException: ", ie);
    Thread.currentThread().interrupt();
} catch (ExecutionException ee) {
    logger.error("ExecutionException: ",ee);
}

通常,当线程被中断时,无论谁中断了该线程,都希望该线程退出其当前正在执行的操作。

但是,请确保您多次捕获:

catch (InterruptedException | ExecutionException e) {     
  logger.error("An error has occurred: ", e);
  Thread.currentThread().interrupt();
}

我们不希望ExecutionException被“重新中断”。

奖励:
如果您有兴趣,可以玩示例here


干杯

相关问题