I'm reviewing some new code. The program has a try and a finally block only. Since the catch block is excluded, how does the try block work if it encounters an exception or anything throwable? Does it just go directly to the finally block?
相关问题
- 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
A small note on
try
/finally
: The finally will always execute unlessSystem.exit()
is called.try{}
block never ends (e.g. endless loop).Java versions before version 7 allow for these three combinations of try-catch-finally...
finally
block will be always executed no matter of what's going on in thetry
or/andcatch
block. so if there is nocatch
block, the exception won't be handled here.However, you will still need an exception handler somewhere in your code - unless you want your application to crash completely of course. It depends on the architecture of your application exactly where that handler is.
The Java Language Specification(1) describes how
try-catch-finally
is executed. Having no catch is equivalent to not having a catch able to catch the given Throwable.(1) Execution of try-catch-finally