Java FAQ try-finally Statement execution issues

Source: Internet
Author: User


The code is as follows:

public class Example039 {public static void main (string[] args) {Example039 example039 = new Example039 (); System.out.println (EXAMPLE039.OUTPUT1 ()); Example039.output2 ();} Boolean output1 () {try {//... return true;} finally {return false;}} void Output2 () {try {System.out.println ("executes the contents of a try statement, then exits, quits abnormally"); System.exit (0);} finally {System.out.println ("the finally is not executed here, because exit");}}}


Output Result:

False to execute the Try statement content, then exit, and exit abnormally


Program Analysis:

in a try-finally statement, the finally statement block is always executed when control leaves the try statement block. This is true regardless of whether the try statement block ends normally or ends unexpectedly. when both the TRY statement block and the finally statement block end unexpectedly, the cause of an unexpected end in the try statement block is discarded, and the reason for the unexpected end of the entire try-finally statement is the same as the reason that the finally statement block ended unexpectedly. in output1, the unexpected end of a return statement in a try statement block is discarded, and the unexpected end of the try-finally statement is caused by a return in the finally statement block. In a nutshell, the program tries to return True (try), but it finally (finally) returns (return) is false.

Each finally statement block should end normally unless the exception being thrown is unchecked. Never exit a finally statement block with a return, break, continue, or throw, and never allow a checked exception to be propagated outside of a finally statement block.

According to the above analysis, the output of OUTPUT1 is very easy to understand. But Output2 gives an example of the opposite of the above analysis.

The finally statement block does execute regardless of whether the execution of the TRY statement block ends normally or unexpectedly. However, in the OUTPUT2 program, the TRY statement block simply does not end its execution process. The System.exit method stops the current thread and all other threads that die on the spot. The appearance of the finally clause does not give the thread the special permission to continue executing.

when System.exit is called, the virtual machine performs two cleanup tasks before shutting down. First, it performs all the close hook operations, which are already registered on the Runtime.addshutdownhook. This is useful for freeing up resources outside the VM. It is important to close the hooks for behaviors that must occur before the VM exits . The second cleanup task performed by VM execution when System.exit is invoked is related to finalizers.

In summary, System.exit will stop all program threads immediately, and it will not make the finally statement block called, but it will perform a close hook operation before stopping the VM. when the VM is shut down, use the close hook to terminate the external resource. by calling System.halt, you can stop a VM without performing a close hook, but this method is rarely used.


note: This "Java Confusion" series are bloggers reading "Java doubts" The original book after the interpretation of the original book and examples of the adaptation and then write a post to publish. All examples are personally tested and shared on GitHub. Use these examples to motivate yourself to benefit others. At the same time, all the posts in this series will be published in the blogger personal public search "Love Ape" or "ape_it" to facilitate reading. If there is any infringement of the original rights of the author, please inform the blogger in time to delete if the reader has objections to the content of the text or questions are welcome through the blog post or public messages and other ways to discuss together.

Source code Address Https://github.com/rocwinger/java-disabuse


This article from "Winger" blog, declined reprint!

Java FAQ try-finally Statement execution issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.