Is there any condition where finally might not run in java? Thanks.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Also if a deadlock/livelock happens inside
try
block.Here's the code that demonstrates it:
This code produces the following output:
and "finally" never gets printed
In that case the finally will not execute (unless the deprecated
Thread.stop
is called, or an equivalent, say, through a tools interface).System.exit shuts down the Virtual Machine.
"bye" does not print out in above code.
Related to System.exit, there are also certain types of catastrophic failure where a finally block may not execute. If the JVM runs out of memory entirely, it may just exit without catch or finally happening.
Specifically, I remember a project where we foolishly tried to use
This didn't work because the JVM had no memory left for executing the catch block.
Here are some conditions which can bypass a finally block:
Last non-daemon thread exits example:
Output: