在进入finally块之前是否可以检测到是否发生异常? [英] Is it possible to detect if an exception occurred before I entered a finally block?

查看:140
本文介绍了在进入finally块之前是否可以检测到是否发生异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,是否有一种优雅的方式来检测在运行finally块之前是否发生异常?在处理close()语句时,在finally块中需要异常处理是很常见的。理想情况下,我们希望保留这两个异常并将其传播(因为它们都可能包含有用的信息)。我想到这样做的唯一方法是在try-catch-finally范围之外有一个变量来保存引用异常的引用。然后将保存异常传播到finally块中发生的任何事件。



有更优雅的方式吗?也许一个可以揭示这个API的API调用?



以下是我正在谈论的粗略代码:

  Throwable t = null; 
try {
stream.write(buffer);
} catch(IOException e){
t = e; //需要保存这个异常,最终
throw e;
} finally {
try {
stream.close(); //可能抛出异常
} catch(IOException e){
//有没有比保存异常块更好的东西?
if(t!= null){
//将读取的异常传播为原因 - 不是很好,但是看到我的意思。
抛出新的IOException(终于无法关闭块:+ e.getMessage(),t);
} else {
throw e; // just pass it up
}
} // end close
}

显然,还有一些其他类似的kludge可能涉及将异常保存为成员变量,从方法返回等等,但我正在寻找一些更优雅的东西。



也许像 Thread.getPendingException()或类似的东西?对于这个问题,是否有其他语言的优雅解决方案?



这个问题实际上是从另一个问题提出了一个有趣的问题。

解决方案

您的想法关于设置一个变量超出try / catch / finally范围是正确的。



一次不能有多个异常传播。


In Java, is there an elegant way to detect if an exception occurred prior to running the finally block? When dealing with "close()" statements, it's common to need exception handling within the finally block. Ideally, we'd want to maintain both exceptions and propagate them up (as both of them may contain useful information). The only way I can think of to do this is to have a variable outside the try-catch-finally scope to save a reference to a thrown exception. Then propagate the "saved" exception up with any that occur in the finally block.

Is there a more elegant way of doing this? Perhaps an API call that will reveal this?

Here's some rough code of what I'm talking about:

Throwable t = null; 
try {   
   stream.write(buffer); 
} catch(IOException e) {
    t = e;   //Need to save this exception for finally
    throw e;
} finally {   
    try {
       stream.close();   //may throw exception
   } catch(IOException e) {
      //Is there something better than saving the exception from the exception block?
      if(t!=null) {
         //propagate the read exception as the "cause"--not great, but you see what I mean.
         throw new IOException("Could not close in finally block: " + e.getMessage(),t);
      } else {
         throw e;  //just pass it up
      }    
   }//end close
}

Obviously, there are a number of other similar kludges that might involve saving the exception as an member variable, returning it from a method, etc... but I'm looking for something a bit more elegant.

Maybe something like Thread.getPendingException() or something similar? For that matter, is there an elegant solution in other languages?

This question actually spawned from comments in another question that raised an interesting question.

解决方案

Your idea about setting a variable outside the scope of the try/catch/finally is correct.

There cannot be more than one exception propagating at once.

这篇关于在进入finally块之前是否可以检测到是否发生异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆