【问题标题】:Fail fast finally clause in JavaJava中的快速失败最终子句
【发布时间】:2010-09-09 03:51:27
【问题描述】:

有没有办法从 finally 子句中检测到异常正在被抛出?

请看下面的例子:


try {
    // code that may or may not throw an exception
} finally {
    SomeCleanupFunctionThatThrows();
    // if currently executing an exception, exit the program,
    // otherwise just let the exception thrown by the function
    // above propagate
}

或者你唯一能做的就是忽略其中一个例外?

在 C++ 中,它甚至不允许您忽略其中一个异常,而只是调用 terminate()。大多数其他语言使用与 java 相同的规则。

【问题讨论】:

    标签: java exception fault-tolerance


    【解决方案1】:

    如果您发现自己这样做了,那么您的设计可能有问题。 “finally”块的想法是,无论方法如何退出,您都希望完成某些事情。在我看来,您根本不需要 finally 块,而应该只使用 try-catch 块:

    try {
       doSomethingDangerous(); // can throw exception
       onSuccess();
    } catch (Exception ex) {
       onFailure();
    }
    

    【讨论】:

      【解决方案2】:

      不,我不这么认为。 catch 块将在 finally 块之前运行完成。

      try {
          // code that may or may not throw an exception
      } catch {
      // catch block must exist.
      finally {
          SomeCleanupFunctionThatThrows();
      // this portion is ran after catch block finishes
      }
      

      否则,您可以添加异常代码将使用的 synchronize() 对象,您可以检查 finally 块,这将帮助您识别是否在单独的线程中运行异常。

      【讨论】:

      • 这是错误的,绝对不需要有一个catch块。
      【解决方案3】:

      您的意思是您希望 finally 块根据 try 块是否成功完成而采取不同的行动?

      如果是这样,你总是可以这样做:

      boolean exceptionThrown = false;
      try {
          // ...
      } catch(Throwable t) {
          exceptionThrown = true;
          // ...
      } finally {
          try {
              SomeCleanupFunctionThatThrows();
          } catch(Throwable t) { 
              if(exceptionThrown) ...
          }
      }
      

      不过,这变得相当令人费解……您可能想要想办法重组您的代码,让这一切变得不必要。

      【讨论】:

        【解决方案4】:

        设置一个标志变量,然后在 finally 子句中检查它,如下所示:

        boolean exceptionThrown = true;
        try {
           mightThrowAnException();
           exceptionThrown = false;
        } finally {
           if (exceptionThrown) {
              // Whatever you want to do
           }
        }
        

        【讨论】:

        • 你太快了……你抢了我的答案
        【解决方案5】:

        如果一个函数抛出并且你想捕获异常,你必须将函数包装在一个 try 块中,这是最安全的方法。所以在你的例子中:

        try {
            // ...
        } finally {
            try {
                SomeCleanupFunctionThatThrows();
            } catch(Throwable t) { //or catch whatever you want here
                // exception handling code, or just ignore it
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2015-02-25
          • 2012-06-21
          • 1970-01-01
          • 2014-08-24
          • 2020-11-12
          • 1970-01-01
          • 2012-06-22
          • 2018-09-26
          • 2012-08-17
          相关资源
          最近更新 更多