java - 要么重新中断此方法,要么重新抛出声纳中的“InterruptedException 问题”

标签 java exception try-catch throws interrupted-exception

在我的方法之一中,中断异常和执行异常即将到来。
我像这样输入了 try catch 。

try{

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

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


//monitoringexception extends RunTimeException

同样在我的方法中,我抛出了 InterruptedException,ExecutionException
我在声纳中遇到严重错误 - 重新中断此方法或重新抛出“InterruptedException

有人知道怎么修这个东西吗。

请立即帮助。

最佳答案

将“重新中断”作为最佳实践:

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

干杯

关于java - 要么重新中断此方法,要么重新抛出声纳中的“InterruptedException 问题”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54434976/

相关文章:

java - 带有图像/jpeg 的 Android HttpURLConnection POST 请求

java - TCLBLEND 失败 - Centos 6.4

Java - 使用 HashMap 和 List<> 作为键

java - Android 媒体播放器返回 NullPointerException

python-3.x - 使用 Python 模块 aiohttp 捕获 http 错误连接的正确方法是什么?

c++ - catch(...) 总是有意义吗?

java - 如何抛出 StackOverflowException

java - 如何使用带有不同 Java Controller 名称的 swagger-codegen 生成 API?

c# - "throw;"本身有什么作用?

c# - 如何 XML 序列化对象列表数组?