Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Does finally in Java have to be executed?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "will the finally in the Java be implemented?" in the daily operation, I believe that many people have doubts about whether the finally in the Java will be implemented. The editor has consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "will the finally in the Java be implemented?" Next, please follow the editor to study!

Brief introduction

As we all know, finally, as part of exception handling, can only be followed by a statement block immediately after the try/catch statement, indicating that the statement, "under normal circumstances," will eventually be executed (with or without an exception) and is often used in situations where resources need to be released. So do we have to run finally code blocks when our application is running? Actually, no, there are several situations in which our finally code block will not run.

Case 1 where the finally code block will not run: the code flow does not enter the try statement block

This is also the best case to understand, if the code flow does not enter the try code block, then 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 running result is:

Enter main block

Case 2: System.exit (int) exit program is used

After entering the try or catch block, the System.exit (int) exit program is used.

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 running result is:

Enter main block

Enter try block

However, if System.exit (int) is executed after the try code block exception statement, the finally will still be executed because there is no opportunity to execute System.exit (int), and the program has exited, such as:

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 running result is:

Enter main block

Enter try block

Enter catch block

Enter finally block

Case 3: the thread in which the program is located dies

In the case of the current thread death, the statements in the finally will not be executed, such as interfering with the interrupt, or kill the thread outside the program, or aborting unexpectedly.

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 10 seconds, and then kill the thread Thread.sleep (10 * 1000) during the execution of the task; I = I / I } catch (Exception e) {System.out.println ("enter catch block");} finally {System.out.println ("enter finally block");}}

Here, in hibernation, use the kill command to kill the thread, simulate an abnormal exit, and run the result as follows:

Enter main block

Enter try block

It is worth noting here that we often get some critical resource in the try statement block and then release the resource in the finally statement block. At this point, if the program is interrupted abnormally after the resource is fetched normally, we do not release the resource normally, which may cause the resource to be occupied indefinitely. Therefore, other solutions should be considered here, such as setting a usage time for the resource, etc., and automatically withdraw the resource when it expires.

Case 4: other abnormal exits

There are other abnormal exits (same as above, no demonstration), which can also cause finally code blocks not to be executed, such as physically turning off power, shutting down CPU, and so on. In fact, these often occur in the development and production environment. For example, in development, after a server acquires the lock, it accidentally loses power or goes down (the lock is not released successfully), and then other machines cannot get the lock (if the lock has no time limit), which eventually leads to systematic problems. When you're done, you developers don't know what's going on, so you restart all the services, solve the problem, and finally say, "Ah!" it's better to restart Dafa.

At this point, the study of "will the finally in Java be implemented" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

  • How does JavaScript always carry a frame?

    This article mainly introduces how JavaScript always brings a framework, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article. Always bring the frame

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report