Java finally block execution timing analysis

Source: Internet
Author: User
Tags finally block try catch

The Java finally keyword is typically used with a try catch block. Used to do some resource-free operations before the end of the method or when an exception occurs. Recently also seen on the web there are some articles that discuss the order of the Try Catch finally keyword execution, and give the finally block is executed at the end of the method.

These views generally agree that:

1) The Finally keyword is executed before returning to the previous level method after the program return statement, where the return value is saved in a staging area, and after the part of the finally block is executed, the value of the staging area is returned.

2) If there is a return value in the finally block, replace the value in the staging area with the return statement in the previous try or catch block in the program.

But the question is really this, we think carefully, the JVM is at runtime to interpret the bytecode instruction execution, when he executes to the return statement, he does not know if there is no finally block, if there is no finally block what to do, Whether it is a bytecode instruction or a computer instruction should be clear, the JVM is not so smart, the same instruction must be clear and will not contain two meanings. So for the return statement to run regardless of what happens, unity pops up the contents of the stack and returns to the calling method.

The finally statement is correct before the method returns, but execution after the try or catch block is not true. Let's look at an example below.

1.try Catch-finally example:

 Public classFinallytest { Public Static voidMain (string[] args) {intR =test ();    System.out.println (R); }     Public Static intTest () {Try {
System.out.println ("Try"); return 1/0; return0; } Catch(Exception e) {System.out.println ("Exception"); return100; }finally{System.out.println ("Finally"); } }}

The return 0 statement is used in the try block, and the result of the program is:

Try
Finally
0

Using the return 1/0 statement in the try block, the result of the program's operation is:

exception
Finally
100

In fact, by running the result we can see that the finally block is executed after the other statements in the try or catch block before the return statement. In other words, the order of the program does not match our execution order, because the JVM interprets the bytecode, so we need to look at how the C # compiler compiles this code to see what bytecode it generates.

2. Program-generated partial bytecode: (Java bytecode Directive refer to)

1   Public Static inttest ();2 Descriptor: () I3 Flags:acc_public, Acc_static4 Code:5stack=2, locals=2, args_size=060:getstatic #20//Field Java/lang/system.out:ljava/io/printstream;73:LDC #36//String Try85:invokevirtual #38//Method java/io/printstream.println: (ljava/lang/string;) V98:getstatic #20//Field Java/lang/system.out:ljava/io/printstream;Ten      11:LDC #41                //String finally One         13:invokevirtual #38                 //Method java/io/printstream.println: (ljava/lang/string;) V A         16:iconst_0 -         17:ireturn -18: Astore_0 the19:getstatic #20//Field Java/lang/system.out:ljava/io/printstream; -22:LDC #43//String Exception -24:invokevirtual #38//Method java/io/printstream.println: (ljava/lang/string;) V -27:getstatic #20//Field Java/lang/system.out:ljava/io/printstream; +30:LDC #41                 //String finally -32:invokevirtual #38                 //Method java/io/printstream.println: (ljava/lang/string;) V +35:Bipush A37: ireturn  at38: Astore_1 -39:getstatic #20//Field Java/lang/system.out:ljava/io/printstream; -42:LDC #41//String finally -44:invokevirtual #38//Method java/io/printstream.println: (ljava/lang/string;) V -47: Aload_1 -48: Athrow in Exception Table: - From to target type to0 8 Class java/lang/Exception +0 8 38 any -All

From the red part we can see: 10, 11 lines corresponding to the Finally block statement instruction, 16,17 corresponding to the return 0 instruction, after the TRY block other statements, before return. And 19,20 corresponds to the finally block instruction, 21,22 corresponding to the return 100 statement instruction, after the catch other statements, return before, so we can see that what is happening behind this is the C # compiler for us to do all this, as for the exception that occurs in the program, The JVM will be executed from the exception table to find the address where the exception is handled.

So we can conclude that the statements in the finally block will have the C # compiler inserted after the try block and the CATCH block return statement before the other statements. That's why it happens. Whether executing a try block or executing a catch block, finally executes a finally block before the method returns.

Java finally block execution timing analysis

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.