有没有办法避免finally子句被执行? [英] Is there any way to avoid the finally clause to get executed?
问题描述
可能重复:
是否总是运行finally块?
I得知try catch语句的finally子句总是执行。但有些人告诉我,可以避免执行它(删除它不是一个选项)。
I learned that the finally clause of a try catch statement, executes always. But some guy said to me that it is possible to avoid executing it(removing it is not an option).
- 有人怎么可能?
-Does someone how is that possible?
- 我很想知道为什么有人想避免执行它?
-Also i am curious in knowing why would someone want to avoid to execute it?
推荐答案
在 finally
块中杀死未捕获的异常,或者杀死整个JVM(杀死线程等)。
Kill it with an uncaught exception within the finally
block, or kill the overall JVM (which kills the thread, among other things).
除了糟糕的设计外,没有充分的理由停止执行 finally
块。如果它不应该每次运行,那么不要把它放在 finally
块中。
There is no good reason to stop the execution of a finally
block except poor design. If it's not supposed to run every time, then don't put it in a finally
block.
使用下面的测试代码,我运行两个不同的场景,看看杀死线程
时会发生什么:
Using the below test code, I run two different scenarios to see what happens when killing the Thread
:
- 启动
线程
和sleep
2的主线程秒。在Thread
中,几乎立即进入finally
块然后再睡5秒钟。一旦主线程完成等待,使用stop
终止线程
。 - 启动
线程
并休眠2秒。在Thread
中,在进入finally
块之前休息5秒,然后在终于
给它一个被杀的机会。
- Start the
Thread
andsleep
the main thread for 2 seconds. Within theThread
, pretty much immediately enter thefinally
block and then sleep for 5 seconds. Once the main thread is finished waiting, kill theThread
usingstop
. - Start the
Thread
and sleep 2 seconds. Within theThread
, sleep 5 seconds before entering thefinally
block and then sleep some more within thefinally
to give it a chance to be killed.
在第一种情况下,结果是 finally
块停止执行。
在第二种情况下,结果是 finally
块完全执行,并且在 Thread
上执行是停止
ped不少。
In the first case, the result is that the finally
block stops executing.
In the second case, the result is that the finally
block executes completely, and on the Thread
that was stop
ped no less.
输出(注意为所有输出添加的当前线程的名称):
Output (note the name of the current thread added for all output):
thread-starting [main]
trying [Thread-0]
catching [Thread-0]
finally-sleeping [Thread-0]
thread-stopped [main]
[main]
thread-starting [main]
trying-sleeping [Thread-1]
thread-stopped [main]
finally-sleeping [Thread-1]
finally-done [Thread-1]
代码:
public class Main
{
public static void main(String[] args)
{
testThread(new TestRunnable());
println("");
testThread(new TestRunnable2());
}
private static void testThread(Runnable runnable)
{
Thread testFinally = new Thread(runnable);
println("thread-starting");
testFinally.start();
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
println("main-interrupted...");
}
testFinally.stop();
println("thread-stopped");
}
private static class TestRunnable implements Runnable
{
@Override
public void run()
{
try
{
println("trying");
throw new IllegalStateException("catching");
}
catch (RuntimeException e)
{
println(e.getMessage());
}
finally
{
println("finally-sleeping");
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
println("finally-interrupted");
}
println("finally-done");
}
}
}
private static class TestRunnable2 implements Runnable
{
@Override
public void run()
{
try
{
println("trying-sleeping");
Thread.sleep(5000);
}
catch (InterruptedException e)
{
println("trying-interrupted");
}
finally
{
println("finally-sleeping");
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
println("finally-interrupted");
}
println("finally-done");
}
}
}
private static void println(String line)
{
System.out.printf("%s [%s]%n", line, Thread.currentThread().getName());
System.out.flush();
}
}
这篇关于有没有办法避免finally子句被执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!