In Java, is the “finally” block guaranteed to be called (in the main method)?

前端 未结 10 2044
执念已碎
执念已碎 2020-12-06 09:17

I\'m a Java rookie and I was wondering, if I have the following typical Java code

public class MyApp {
  public static void main(String[] args) {
    try {
          


        
10条回答
  •  被撕碎了的回忆
    2020-12-06 10:19

    In a word, yes.

    Code in the finally block in Java always executes unless:

    • The JVM exits during the try or catch block
    • The thread running the code is interrupted or killed during the try or catch block

    (from: http://java.sun.com/docs/books/tutorial/essential/exceptions/finally.html)

    So, unless you explicitly call System.exit(int), or kill the process or thread externally, you can rely on it.

提交回复
热议问题