Java 即使你抛出一个新的异常,finally 块也会运行吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4264874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does a finally block run even if you throw a new Exception?
提问by jax
In this code will someVar
be set even if the catch block is executed and the second Exception is thrown?
在这段代码中,someVar
即使执行了 catch 块并抛出了第二个异常,也会设置吗?
public void someFunction() throws Exception {
try {
//CODE HERE
} catch (Exception e) {
Log.e(TAG, "", e);
throw new Exception(e);
} finally {
this.someVar= true;
}
}
采纳答案by GaryF
Yes, the finally blocks always runs... except when:
是的,finally 块总是运行......除非:
- The thread running the try-catch-finally block is killed or interrupted
- You use
System.exit(0);
- The underlying VM is destroyed in some other way
- The underlying hardware is unusable in some way
- 运行 try-catch-finally 块的线程被杀死或中断
- 你用
System.exit(0);
- 底层 VM 以其他方式销毁
- 底层硬件在某些方面无法使用
Additionally, if a method in your finally block throws an uncaught exception, then nothing after that will be executed (i.e. the exception will be thrown as it would in any other code). A very common case where this happens is java.sql.Connection.close()
.
此外,如果 finally 块中的方法抛出未捕获的异常,则之后不会执行任何操作(即异常将像在任何其他代码中一样抛出)。发生这种情况的一个非常常见的情况是java.sql.Connection.close()
.
As an aside, I am guessing that the code sample you have used is merely an example, but be careful of putting actual logic inside a finally block. The finally block is intended for resource clean-up (closing DB connections, releasing file handles etc), not for must-run logic. If it must-run do it before the try-catch block, away from something that could throw an exception, as your intention is almost certainly functionally the same.
顺便说一句,我猜您使用的代码示例只是一个示例,但要小心将实际逻辑放入 finally 块中。finally 块用于资源清理(关闭数据库连接、释放文件句柄等),而不是用于必须运行的逻辑。如果它必须在 try-catch 块之前运行,请远离可能引发异常的东西,因为您的意图几乎可以肯定在功能上是相同的。
回答by froadie
Yes.
是的。
See the documentation:
请参阅文档:
The finally block alwaysexecutes when the try block exits.
finally 块总是在 try 块退出时执行。
Exceptions:
例外:
Note: If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
注意:如果在执行 try 或 catch 代码时 JVM 退出,则 finally 块可能不会执行。同样,如果执行 try 或 catch 代码的线程被中断或终止,即使应用程序作为一个整体继续运行,finally 块也可能不会执行。
回答by urmalp
The finally block always executes when the try block exits.unless you've System.exit(0) in your try or catch.
finally 块总是在 try 块退出时执行。除非您在 try 或 catch 中有 System.exit(0)。
回答by Vladimir Ivanov
Yes. finally
block executes always except the case you call System.exit() because it stops Java VM.
是的。finally
块总是执行,除非您调用 System.exit() ,因为它会停止 Java VM。
回答by yug
Finally is always executed, no matter what you case is i.e
最后总是被执行,无论你的情况是什么,即
- try-catch-finally block
- throws
- try-catch-finally 块
- 投掷
For unchecked exceptions java does not mandate, error handling. this being the reason, if an unchecked exception occurs in finally block then and no handling is done for that, then code written below this point (where error occurred) will not be executed.
对于未经检查的异常,java 没有强制要求,错误处理。这就是原因,如果在 finally 块中发生未经检查的异常并且没有对此进行处理,那么在此点(发生错误的地方)下方编写的代码将不会执行。
So i suggest to always handle all the exceptions may it be checked or unchecked. this way you can make sure that code block in finally is also executed no matter if unchecked exception also occurs. you have a place in sub nest catch and finally block to get your necessary work done.
所以我建议总是处理所有的异常,无论它是被检查的还是未检查的。这样你就可以确保 finally 中的代码块也被执行,无论是否发生未经检查的异常。您在子嵌套捕获中占有一席之地,并最终阻止以完成必要的工作。
回答by Vasanth Umapathy
Finally, block always executes.
最后,块总是执行。
public class ExceptionTest {
public static void someFunction(String input) throws Exception {
try {
if( input.equals("ABC") ) {
System.out.println("Matched");
}
} catch (Exception e) {
throw new Exception(e);
} finally {
System.out.println("Input Is "+input+" Finally Executed!!!");
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println("********* Test with VALUE ********* ");
someFunction("ABC");
System.out.println("\r\n********* Test with NULL ********* ");
someFunction(null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}