Java-Foundation-"Three" try/catch/finally

Source: Internet
Author: User
Tags finally block throwable

Original address:

https://my.oschina.net/bieber/blog/703251

First, single-layer Try/catch

 Public int Test (int A,int  b) {        try{            return A +b ;        } Catch (Exception e) {            thrownew  customexception ();        }}

See how this is handled by JAVAP-V after the JVM is compiled into a class byte code try/catch

 Public intTestint,int); Flags:acc_public Code:stack=2, locals=4, args_size=3 0:iload_1//press the first int parameter into the queue (the first entry parameter)1:iload_2//press the second int parameter into the queue (second entry)2:iadd//The first and second parameters in the pop-up queue are added and the addition results are pressed into the queue3:ireturn//pops up the first element of the queue and return. 4:astore_3//here is the logic of try start5:New#3//class Com/bieber/demo/customexception8: DUP9:invokespecial #4//Method com/bieber/demo/customexception. " <init> ":() V12:athrow//pops the first element in the queue and throws it as an exception to the entire method bodyException table:from to target type0 3 4 Class java/lang/Exception Linenumbertable:line13:0 Line14:4 Line15:5localvariabletable:start Length Slot Name Signature5 8 3 e ljava/lang/Exception; 0 13 0 Thislcom/cainiao/cilogisticservice/Exceptionclass; 0 13 1a I0 13 2b I stackmaptable:number_of_entries= 1Frame_type= 68/*Same_locals_1_stack_item*/Stack= [classJava/lang/exception]
View Code

The above is the result of the test method JVM compilation, the above Code block is the content of the entire method body, and from 0-3 can be regarded as the normal logic of the method body, 4-12 can be regarded as Try/catch block, from the method body instructions to see, the normal execution 3 of the place is complete, Instead of executing the 4-12 directive.

It concludes that blocks of try/catch code are not executed in normal logic, so adding blocks to the code try/catch does not affect the efficiency of the execution of the code, because there is no extra instruction to execute, and only an exception occurs when an exception is executed.

The above JVM compiles bytecode in addition to blocks of code, and Code there are Exception table blocks of code that can be seen from the contents of this block of code, which contains four columns (From,to,target,type), which from and to represents this try/catch Where the code block starts and ends, you can see that the try/catch code block above is from 0-3 of the code block, that is, Code from loading the first int value to the code block that returns the result, target indicating try/catch where the code block execution logic begins, such as the above representation from Code Starting at 4, which astore_3 is where the instruction starts, until the athrow instruction is executed, and in Exception table the row there is a type column that indicates that this exception type is used to try/catch present multiple contents in a block of code to catch match the correct exception type.

Second try corresponds to multiple catch

  Public intTestintAintb) {        Try{            returnA +b; }Catch(Exception e) {a++; Throw Newcustomexception (); }Catch(Throwable t) {b++; Throw Newcustomexception (); }    }

The result of the JVM's compilation of the above code:

  Public intTestint,int); Flags:acc_public Code:stack=2, locals=4, args_size=3 0: Iload_11: Iload_22: Iadd3: Ireturn4: Astore_35:iinc 1, 1 8:New#3//class Com/bieber/demo/customexception11: DUP12:invokespecial #4//Method com/bieber/demo/customexception. " <init> ":() V15: Athrow16: Astore_317:iinc 2, 1 20:New#3//class Com/cainiao/cilogisticservice/customexception23: DUP24:invokespecial #4//Method com/cainiao/cilogisticservice/customexception. " <init> ":() V27: Athrow Exception table:from to target type0 3 4 Class java/lang/Exception0 3 Class java/lang/throwable linenumbertable:line13:0 Line14:4 Line15:5 Line16:8 Line17:16 Line18:17 Line19:20localvariabletable:start Length Slot Name Signature5 3 e ljava/lang/Exception; 3 T ljava/lang/Throwable; 0 28 0 Thislcom/cainiao/cilogisticservice/Exceptionclass; 0 28 1a I0 28 2b I stackmaptable:number_of_entries= 2Frame_type= 68/*Same_locals_1_stack_item*/Stack= [classjava/lang/Exception] Frame_type= 75/*Same_locals_1_stack_item*/Stack= [classJava/lang/throwable]
View Code

Compared with the above content will find that in the Code more than a astore_3/athrow block, and in Exception table a row, think through the above explanation, the purpose of this extra line should know what to use, because I became in the catch throw outside, there is a ++ Operation, you can see that there astore_3/athrow is an extra instruction in the block, so it iinc is understandable that try/catch a child code block in the JVM is executed when the condition satisfies (a matching catch exception occurs).

Let me tidy up the JVM processing process when there is an exception (which is said to be the try/catch exception):

1, in the Try/catch exception 2, the JVM will go to ' Exception table ' to find the matching exception type 3, assuming the match, then read From,to,target, get to execute the ' try/catch ' block of instructions (whether or not thrown, See if there is a athrow directive).

Three, try/finally block's execution processing

 Public int Test (int A,int  b) {        try{            return A +b ;        } Catch (Exception e) {            a++            ; Throw New customexception ();        } finally {            b+ +;        }    }

JVM-Compiled directives:

 Public intTestint,int); Flags:acc_public Code:stack=2, locals=5, args_size=3 0: Iload_11: Iload_22: Iadd3:istore_3//stores the top element of the stack in the third position of the local variable array4:iinc 2, 1//Executive b++7:iload_3//Press the value of the third position of the local variable into the top of the stack8:ireturn//pops up the top of the stack and returns9: Astore_310:iinc 1, 1//a++13:New#3//class Com/bieber/demo/customexception16: DUP17:invokespecial #4//Method com/bieber/demo/customexception. " <init> ":() V20: Athrow21:astore 4 23:iinc 2, 1//b++26:aload 4 28: Athrow Exception table:from to target type0 4 9 Class java/lang/Exception0 4 21 any9 23 21Any linenumbertable:line13:0 Line18:4 Line14:9 Line15:10 Line16:13 Line18:21localvariabletable:start Length Slot Name Signature3 e ljava/lang/Exception; 0 29 0 Thislcom/cainiao/cilogisticservice/Exceptionclass; 0 29 1a I0 29 2b I stackmaptable:number_of_entries= 2Frame_type= 73/*Same_locals_1_stack_item*/Stack= [classjava/lang/Exception] Frame_type= 75/*Same_locals_1_stack_item*/Stack= [classJava/lang/throwable]
View Code

From the above code, you will find that there Exception table are two lines, in fact, we only have a block in the code try/catch , and here there are three, then what is the other two do? Can see the two more lines of the type are any , here is the any expression of any exception type, the extra first line, is from 0-4 , represents 0-4 the instruction between the exception, will 21 start execution from the instruction, found that the execution is b++ (finally) The second line of content, the more out of which is, indicates that the exception that occurs during the 9-23 9-23 execution of the instruction is also executed from the 21 line (which is also the execution finally of the content), and 9-23 is actually catch the code logic. The above is the exception that will trigger finally the execution of the code, under normal circumstances will find 4 the location of the execution finally of the content, and then execute the ireturn instructions, here can be concluded that the JVM processing finally is actually for the normal command queue increased finally The instructions for the code block, and the instructions for adding code blocks to the exception, finally cause it to be fianlly executed anywhere, in fact, the command queue is redundant (in fact, the idea is relatively simple).

Java-Foundation-"Three" try/catch/finally

Related Article

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.