Java finally 什么时候执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16412802/
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
When finally is executed?
提问by VansFannel
I have this java code with nested try:
我有这个带有嵌套尝试的java代码:
try
{
try
{
[ ... ]
{
catch (Exception ex)
{
showLogMessage(ex);
return;
}
while (condition == true)
{
try
{
[ ... ]
{
catch (Exception ex)
{
showLogMessage(ex);
continue;
}
[ ... ]
}
}
catch (NumberFormatException e)
{
showLogMessage(e);
}
finally
{
doSomeThingVeryImportant();
}
I want to know if finally
is always executed when I get an exception. I ask this because catch blocks have return
or continue
statements.
我想知道finally
当我遇到异常时是否总是执行。我问这个是因为 catch 块有return
orcontinue
语句。
When is doSomeThingVeryImportant()
executed? When I get an Exception
on when I get a NumberFormatException
?
什么时候doSomeThingVeryImportant()
执行?当我Exception
得到一个NumberFormatException
?
I only want if after any catch block is executed, the finally block is executed also.
我只想要在执行任何 catch 块之后,也执行 finally 块。
采纳答案by tostao
回答by Bhushan Bhangale
Yes finally always gets executed and its because one can program to close/handle resourcesin case of exceptions or no exception without fail.
是的,最终总是会被执行,这是因为人们可以编程以在出现异常或没有异常时关闭/处理资源而不会失败。
http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
Update to answer the question in comment about System.exit()
更新以回答评论中的问题 System.exit()
The finally block will not execute in case of System.exit()
unless it fails with some SecurityException
.
finally 块在 的情况下不会执行,System.exit()
除非它因 some 失败SecurityException
。
Update after question changed. No matter what the exception is finally block will always execute.
问题改变后更新。不管是什么异常,finally 块都会一直执行。
If its Exception
then your catch block will not execute as it catches only NumberFormatException
如果它Exception
那么您的 catch 块将不会执行,因为它只捕获NumberFormatException
回答by John Snow
The finally block always executes when the try block exits.
This ensures that the finally block is executed even if an unexpected exception occurs
Taken from here: http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
取自这里:http: //docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
Further, the page explains that the finally block may not
be executed if the JVM exists while the try or catch code is being executed, or if the thread executing the try/catch is interrupted or killed.
此外,该页面解释了may not
如果在执行 try 或 catch 代码时 JVM 存在,或者如果执行 try/catch 的线程被中断或终止,则将执行finally 块。
So unless you may kill the JVM or the try/catch is beeing executed in a thread, the finally block will always be executed
因此,除非您可能杀死 JVM 或在线程中执行 try/catch,否则将始终执行 finally 块
回答by Carlos Landeras
Yes, Finally always executes. With exception and with NO exception.
是的,最终总是执行。无一例外。
It's the way to be sure some portion of code get alwaysexecuted.
这是确保始终执行某些代码部分的方法。
Used for example, to dispose objects, to close opened server connections and that kind of stuff.
例如,用于处理对象、关闭打开的服务器连接等等。
Check this link from oracle:
从 oracle 检查此链接:
http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
回答by Luke Grab-Bag English
Yes, finally
blocks are always executed.
是的,finally
块总是被执行。
Source: http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
来源:http: //docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
回答by Suresh Atta
Yes, finally blocks are always executed.But terms and conditions apply .
是的,finally 块总是被执行。但条款和条件适用。
Untill unless you call System.exit();
除非你调用System.exit();
回答by killscreen
Yes; or, at least, as close to "always" as possible. (So, even if you have a return
or another throw
.)
是的; 或者,至少,尽可能接近“总是”。(所以,即使你有一个return
或另一个throw
。)
If your process is killed, or your program gets stuck in a deadlock or infinite loop, or your device is struck by a meteor, then program flow will not reach the finally
block.
如果您的进程被杀死,或者您的程序陷入死锁或无限循环,或者您的设备被流星击中,那么程序流将无法到达finally
块。
回答by Maximin
No, finally won't get execute always. There are two situations where Finally won't get execute.
不,最终不会总是执行。有两种情况最终不会执行。
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 块也可能不会执行。
For your question, if these two things are not there, whatever inside your finally will get executed, that's sure.
对于你的问题,如果这两件事都不存在,那么你最终会执行什么,那是肯定的。
回答by Madhusudan Joshi
Say your code block is something like this :
假设你的代码块是这样的:
try
{
try
{
System.out.println("first try block");
}
catch (Exception ex)
{
System.out.println("first cathc block");
return;
}
while (true)
{
try
{
System.out.println("second try block...");
}
catch (Exception ex)
{
System.out.println("second catch block");
continue;
}
System.out.println("end of loop");
break;
}
}
catch (NumberFormatException e)
{
System.out.println("last catch block");
}
finally
{
System.out.println("finally block");
}
If you run this then the output that you will get is :
如果您运行它,那么您将获得的输出是:
first try block
second try block...
end of loop
finally block
So finally block
gets executed anyway.
所以finally block
无论如何都会被执行。
回答by Jainendra
The finally block, if used, is placed after a try block and the catch blocks that follow it. The finally block contains code that will be run whether or not an exception is thrown in a try block. The general syntax looks like this:
finally 块(如果使用)放置在 try 块和跟随它的 catch 块之后。finally 块包含无论是否在 try 块中抛出异常都会运行的代码。一般语法如下所示:
public void someMethod{
Try {
// some code
}
Catch(Exception x) {
// some code
}
Catch(ExceptionClass y) {
// some code
}
Finally{
//this code will be executed whether or not an exception
//is thrown or caught
}
}
There are 4 potential scenarios here:
这里有 4 种可能的场景:
The try block runs to the end, and no exception is thrown. In this scenario, the finally block will be executed after the try block.
An exception is thrown in the try block, which is then caught in one of the catch blocks. In this scenario, the finally block will execute right after the catch block executes.
An exception is thrown in the try block and there's no matching catch block in the method that can catch the exception. In this scenario, the call to the method ends, and the exception object is thrown to the enclosing method - as in the method in which the try-catch-finally blocks reside. But, before the method ends, the finally block is executed.
Before the try block runs to completion it returns to wherever the method was invoked. But, before it returns to the invoking method, the code in the finally block is still executed. So, remember that the code in the finally block willstill be executed even if there is a return statement somewhere in the try block.
try 块运行到最后,没有抛出异常。在这种情况下,finally 块将在 try 块之后执行。
在 try 块中抛出异常,然后在其中一个 catch 块中捕获该异常。在这种情况下,finally 块将在 catch 块执行后立即执行。
在 try 块中抛出异常,并且方法中没有匹配的 catch 块可以捕获该异常。在这种情况下,对方法的调用结束,异常对象被抛出到封闭方法——就像在 try-catch-finally 块所在的方法中一样。但是,在方法结束之前,会执行 finally 块。
在 try 块运行完成之前,它返回到调用该方法的地方。但是,在它返回到调用方法之前,finally 块中的代码仍然被执行。所以,请记住,即使在 try 块中的某处有 return 语句,finally 块中的代码仍然会被执行。
Update:Finally ALWAYS gets executed, no matter what happens in the try or catch block (fail, return, exception, finish etc.).
更新:无论在 try 或 catch 块(失败、返回、异常、完成等)中发生什么,最后总是被执行。