In the Java language, will finally be executed?

brief introduction

   we all know that finally, as a part of exception handling, it can only be followed by a statement block immediately after the try/catch statement, indicating that this statement will be executed in the end "under normal circumstances" (whether there is an exception thrown or not), which is often used when resources need to be released. So, when our application is running, will the finally code block run? In fact, it is not. In the following cases, our finally code block will not run.

finally, the code block will not run

Case 1: the code flow does not enter the try{} statement block

   this is also the best understood case. If the code flow does not enter the try code block, the corresponding catch and finally code blocks will not be executed naturally.

	public static void main(String[] args) {
        int i = 0;
        System.out.println("enter main block");
        boolean flag = false;
        if (flag) {
            try {
                System.out.println("enter try block");
                i = i / i;
            } catch (Exception e) {
                System.out.println("enter catch block");
            }finally {
                System.out.println("enter finally block");
            }
        }
    }

The operation result is:

enter main block

Case 2: system exit(int)

   after entering the try or catch block, the system Exit (int) exits the program.

	public static void main(String[] args) {
        int i = 0;
        System.out.println("enter main block");
        try {
            System.out.println("enter try block");
            System.exit(0);
            i = i / i;
        } catch (Exception e) {
            System.out.println("enter catch block");
        } finally {
            System.out.println("enter finally block");
        }
    }

or

public static void main(String[] args) {
        int i = 0;
        System.out.println("enter main block");
        try {
            System.out.println("enter try block");
            i = i / i;
        } catch (Exception e) {
            System.exit(0);
            System.out.println("enter catch block");
        } finally {
            System.out.println("enter finally block");
        }
    }

The operation result is:

enter main block
enter try block

But what if system Exit (int) after the try code block exception statement, finally will be executed because there is no chance to execute system Exit (int), the program has exited, for example:

	public static void main(String[] args) {
        int i = 0;
        System.out.println("enter main block");
        try {
            System.out.println("enter try block");
            i = i / i;
            System.exit(0);
        } catch (Exception e) {
            System.out.println("enter catch block");
        } finally {
            System.out.println("enter finally block");
        }
    }

The operation result is:

enter main block
enter try block
enter catch block
enter finally block

Case 3: the thread where the program is located dies

   when the current thread dies, the statement in finally will not be executed, such as interfering with the interrupt, or external to the program, kill ing the thread, or accidental termination.

	public static void main(String[] args) {
        int i = 0;
        System.out.println("enter main block");
        try {
            System.out.println("enter try block");
            // Simulate the execution of the task for 10s, and then kill the thread during the execution of the task
            Thread.sleep(10 * 1000);
            i = i / i;
        } catch (Exception e) {
            System.out.println("enter catch block");
        } finally {
            System.out.println("enter finally block");
        }
    }

Here, in hibernation, kill the thread with the kill command to simulate abnormal exit. The final running result is:

enter main block
enter try block

It is worth noting here that we often obtain some critical resources in the try statement block, and then release the resources in the finally statement block. At this time, if the program is interrupted abnormally after obtaining the resource normally, we do not release the resource normally, which may lead to the unlimited occupation of the resource. Therefore, we should consider other solutions, such as setting a service time for the resource and automatically recovering it when it expires.

Situation 3: other abnormal exits

   there are other abnormal exits (the reason is the same as above, so it will not be demonstrated), which will also lead to the non execution of finally code blocks, such as physically turning off the power, turning off the CPU, etc. In fact, these often occur in the development and production environment. For example, in the development, after a server obtains the lock, it accidentally loses power or goes down (fails to release the lock), which leads to other machines unable to obtain the lock (if the lock has no time limit), and finally leads to systematic problems. After that, you didn't know what had happened, so you restarted all the services, solved the problem, and finally said, "ah! It's better to restart the big method".

Keywords: Java Back-end

Added by lance1208 on Sun, 20 Feb 2022 19:54:54 +0200