Java Try Catch Finally blocks without Catch
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?
If any of the code in the try block can throw a checked exception, it has to appear in the throws clause of the method signature. If an unchecked exception is thrown, it's bubbled out of the method.
The finally block is always executed, whether an exception is thrown or not.
A small note on try
/ finally
: The finally will always execute unless
System.exit()
is called. try{}
block never ends (eg endless loop). 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
链接地址: http://www.djcxy.com/p/73716.html上一篇: 终于在try catch / finally语句除外的意义是什么?
下一篇: Java尝试捕捉最终块没有捕获