是否可以停止执行 finally 块?
Is it possible to stop execution of finally block?
这没有任何意义,我仍然很想知道是否有可能在 java 中停止执行 finally block?如果是,如何?
例如下面的代码:
public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Yes, I executed successfully." );
}
}
}
我希望控制台上的输出不会像是的,我执行成功了。
Well that seems to be possible I concluded. All answers have something
different to accept that it is possible. So, I am not accepting any
answer specifically. Refer them to reach to conclusion as according to your satisfaction. Thanks to all
who posted answers.
我认为 this 说得最好:
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中的return语句不会阻止finally块的执行。也不会抛出异常。这种行为是 finally 块首先存在的原因。它可以让您可靠地清理资源。
1. A infinite loop above it will stop its execution
2. System.exit() above it will stop it
我还没有遇到你实际需要的情况。但是其典型的面试问答是
System.exit(0)
见
Difference in System. exit(0) , System.exit(-1), System.exit(1 ) in Java
A try
statement with a finally
block is executed by first executing
the try
block. Then there is a choice:
- If execution of the
try
block completes normally, then the finally
block is executed, and then there is a choice:
- If the
finally
block completes normally, then the try
statement completes normally.
- If the
finally
block completes abruptly for reason S
, then the try
statement completes abruptly for reason S
.
- If execution of the
try
block completes abruptly because of a throw of a value V
, then there is a choice:
- If the run-time type of
V
is assignment compatible with a catchable exception class of any catch
clause of the try
statement,
then the first (leftmost) such catch
clause is selected. The value V
is assigned to the parameter of the selected catch
clause, and the
Block of that catch
clause is executed. Then there is a choice:
- If the
catch
block completes normally, then the finally
block is executed. Then there is a choice:
- If the
finally
block completes normally, then the try
statement completes normally.
- If the
finally
block completes abruptly for any reason, then the try
statement completes abruptly for the same reason.
- If the
catch
block completes abruptly for reason R
, then the finally
block is executed. Then there is a choice:
- If the
finally
block completes normally, then the try
statement completes abruptly for reason R
.
- If the
finally
block completes abruptly for reason S
, then the try
statement completes abruptly for reason S
(and reason R
is
discarded).
- If the run-time type of
V
is not assignment compatible with a catchable exception class of any catch
clause of the try
statement,
then the finally
block is executed. Then there is a choice:
- If the
finally
block completes normally, then the try
statement completes abruptly because of a throw of the value V
.
- If the
finally
block completes abruptly for reason S
, then the try
statement completes abruptly for reason S
(and the throw of value
V
is discarded and forgotten).
- If execution of the
try
block completes abruptly for any other reason R
, then the finally
block is executed, and then there is a
choice:
- If the
finally
block completes normally, then the try
statement completes abruptly for reason R
.
- If the
finally
block completes abruptly for reason S
, then the try
statement completes abruptly for reason S
(and reason R
is
discarded).
简而言之,如果 JVM 不退出(通过 System.exit
),那么 finally
块是 always 运行。
但是finally
块可能会异常退出,例如抛出异常,在这种情况下finally
块将只执行部分。
Thread.stop()
导致在目标线程中抛出异常。这是异步发生的,也可能在执行 finally
块时发生。在那种情况下,finally
块将突然完成,但允许它发生的线程继续执行。
try {
try {
...code...
} finally {
...resource cleanup, ThreadDeath thrown here...
}
} catch (ThreadDeath t) {
System.out.println("Thread was ordered to stop, ignoring.");
}
这没有任何意义,我仍然很想知道是否有可能在 java 中停止执行 finally block?如果是,如何? 例如下面的代码:
public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Yes, I executed successfully." );
}
}
}
我希望控制台上的输出不会像是的,我执行成功了。
Well that seems to be possible I concluded. All answers have something different to accept that it is possible. So, I am not accepting any answer specifically. Refer them to reach to conclusion as according to your satisfaction. Thanks to all who posted answers.
我认为 this 说得最好:
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中的return语句不会阻止finally块的执行。也不会抛出异常。这种行为是 finally 块首先存在的原因。它可以让您可靠地清理资源。
1. A infinite loop above it will stop its execution
2. System.exit() above it will stop it
我还没有遇到你实际需要的情况。但是其典型的面试问答是
System.exit(0)
见 Difference in System. exit(0) , System.exit(-1), System.exit(1 ) in Java
A
try
statement with afinally
block is executed by first executing thetry
block. Then there is a choice:
- If execution of the
try
block completes normally, then thefinally
block is executed, and then there is a choice:
- If the
finally
block completes normally, then thetry
statement completes normally.- If the
finally
block completes abruptly for reasonS
, then thetry
statement completes abruptly for reasonS
.- If execution of the
try
block completes abruptly because of a throw of a valueV
, then there is a choice:
- If the run-time type of
V
is assignment compatible with a catchable exception class of anycatch
clause of thetry
statement, then the first (leftmost) suchcatch
clause is selected. The valueV
is assigned to the parameter of the selectedcatch
clause, and the Block of thatcatch
clause is executed. Then there is a choice:
- If the
catch
block completes normally, then thefinally
block is executed. Then there is a choice:
- If the
finally
block completes normally, then thetry
statement completes normally.- If the
finally
block completes abruptly for any reason, then thetry
statement completes abruptly for the same reason.- If the
catch
block completes abruptly for reasonR
, then thefinally
block is executed. Then there is a choice:
- If the
finally
block completes normally, then thetry
statement completes abruptly for reasonR
.- If the
finally
block completes abruptly for reasonS
, then thetry
statement completes abruptly for reasonS
(and reasonR
is discarded).- If the run-time type of
V
is not assignment compatible with a catchable exception class of anycatch
clause of thetry
statement, then thefinally
block is executed. Then there is a choice:
- If the
finally
block completes normally, then thetry
statement completes abruptly because of a throw of the valueV
.- If the
finally
block completes abruptly for reasonS
, then thetry
statement completes abruptly for reasonS
(and the throw of valueV
is discarded and forgotten).- If execution of the
try
block completes abruptly for any other reasonR
, then thefinally
block is executed, and then there is a choice:
- If the
finally
block completes normally, then thetry
statement completes abruptly for reasonR
.- If the
finally
block completes abruptly for reasonS
, then thetry
statement completes abruptly for reasonS
(and reasonR
is discarded).
简而言之,如果 JVM 不退出(通过 System.exit
),那么 finally
块是 always 运行。
但是finally
块可能会异常退出,例如抛出异常,在这种情况下finally
块将只执行部分。
Thread.stop()
导致在目标线程中抛出异常。这是异步发生的,也可能在执行 finally
块时发生。在那种情况下,finally
块将突然完成,但允许它发生的线程继续执行。
try {
try {
...code...
} finally {
...resource cleanup, ThreadDeath thrown here...
}
} catch (ThreadDeath t) {
System.out.println("Thread was ordered to stop, ignoring.");
}