Java medium Try Catch finally return run details

Source: Internet
Author: User
Tags try catch

Let's look at an example (example 1) to explain the try, catch, finally process in Java

[Java]  View plain copy public class trycatchfinally {        @ Suppresswarnings ("finally")        public static final String  Test ()  {           String t =  "";             try {                t =  "Try";                return t;            } catch  (exception e)  {                // result =  "Catch";                t =  "Catch";                return t;           }  finally {               t =   "finally";           }        }        public static void main (String[] args)  {           system.out.print (Trycatchfinally.test ());        }   }  

First the program executes the TRY statement block, assigns the variable t to try, because no exception is found, then executes the finally statement block, assigns the variable t to finally, and then return T, then the value of T is finally, and the value of the last T is Finally, The program result should show finally, but the actual result is try. Why this is so, we might as well first look at this code compiled the corresponding class byte code, see how the virtual machine inside how to execute.

We use Javap-verbose trycatchfinally to display the target file (. class file) bytecode information

System operating environment: Mac OS Lion system 64bit

JDK Information: Java (TM) SE Runtime Environment (build 1.6.0_29-b11-402-11m3527) Java HotSpot (tm) 64-bit Server VM (Build 20.4-b02-4 Mixed mode)

Compiled bytecode part of the information, we only look at the test method, the others first ignore

public static final java.lang.String test (); Code:stack=1, locals=4, args_size=0 0:LDC #16; String 2:astore_0 3:LDC #18; String try 5:astore_0 6:aload_0 7:astore_3 8:LDC #20; String finally 10:astore_0 11:aload_3 12:areturn 13:astore_1 14:ldc #22; String catch 16:astore_0 17:aload_0 18:astore_3 19:LDC #20; String finally 21:astore_0 22:aload_3 23:areturn 24:astore_2 25:ldc #20;      String finally 27:astore_0 28:aload_2 29:athrow Exception table:from to target type 3  8 Class java/lang/exception 3 8 any Linenumbertable:line 5: 0 Line 8:3 Line 9:6 line 15:8 line 9:11 line 10:13 line 12:14 line 13:17 line 15:19 Li NE 13:22 line 14:24 line 15:25 line 16:28 localvariableTable:start Length Slot Name Signature 3 0 T ljava/lang/string;

  1 e ljava/lang/exception; Stackmaptable:number_of_entries = 2 Frame_type = 255/* Full_frame/Offset_delta = locals = [Class JAV A/lang/string] Stack = [Class java/lang/exception] frame_type = * * Same_locals_1_stack_item/stack = [Class Java/lang/throwable]

First look at the localvariabletable information, which defines two variables one is T string type, one is e Exception type

Next look at the code section

line [0-2], assign a value to the No. 0 variable "", i.e. string t= "";

line [3-6], which is the execution of a TRY statement block assignment statement, that is, T = "Try";

Line 7th, which focuses on line 7th, pays the third variable with the value "try" for the first S, but this third variable is not defined, which is rather odd.

line [8-10], assign operation to the NO. 0 variable, namely t= "finally"

line [11-12], return the value of the third variable

With bytecode, we find that in the return block of the try statement, the reference variable (t is the reference type) returned is not the reference variable t defined outside the try statement, but the system redefined a local reference T ', which points to the value of the reference T, which is the try, Even if you point the reference T to the value finally in the finally statement, because return reference is not already t, the corresponding value of the reference T is independent of the return value in the Try statement.

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.