The order of execution of try-catch-finally and return from the point of view of the byte code

Source: Internet
Author: User
Tags finally block goto
The order of execution of try-catch-finally and return from the point of view of the byte code

The whole story is illustrated by an example:

First look at the following example code:

public class Exceptiontest {public
    void  testexception () {

        try{
            inside_try ();
        }
        catch (Exception e) {
            inside_catch (e);
        }
        finally{
            inside_finally ();
        }
    Functions that are called in a try block, catch block, and finally block are public
    void Inside_try () {   } public
    void Inside_catch (Exception e) {  }
    public void inside_finally () {}
}

The javap-c exceptiontest command allows you to see the following byte code for this class:

If the exception is not thrown, then it is executed in the following order:

0:aload_0
 1:invokevirtual #2                  //Method Inside_try: () V
 4:aload_0
 5:invokevirtual #3                  // Method inside_finally: () V
 8:goto
 31:return

That is, the code block in the try is executed first, and then the code block in finally is executed.

If an exception is thrown in a try, the JVM looks for a jump position in the following exception table.

Exception table: From to  target type
        0     4   Class java/lang/exception
        0     4 Any of the   

From the exception table, you can see that there are three exceptions that occur that cause different paths to execute:
First: If a command between 0 and 4 bytes (that is, code in a try block) throws an exception of class java/lang/exception type, it jumps to the 11th byte to begin executing the code in the catch;
Second: If a command between 0 and 4 bytes (that is, code in a try block) throws any type of exception, the jump to byte 24th begins executing the finally code.
Third: If the command (that is, the code in the catch block) between 11 and 17 bytes runs out of any type of exception, it jumps to the 24th byte to begin executing the finally code.

First, if the command (code in the try block) between 0 and 4 bytes throws an exception of class java/lang/exception type, then jumps to byte 11th to begin executing the code in the catch

Instructions are as follows:

      11:astore_1
      12:aload_0
      13:aload_1
      14:invokevirtual #5                  //Method Inside_catch: (ljava/lang/e
xception) v
      17:aload_0
      18:invokevirtual #3                  //Method inside_finally: () v
      21:goto          31
      31:return

Astore_1 will save the thrown exception object to the second element of the local variable array. The remaining lines of instructions are used to invoke the methods in the catch and finally blocks.

The second scenario: if a command between 0 and 4 bytes (that is, code in a try block) throws any type of exception, then jumps to byte 24th to begin executing the code in finally

Instructions are as follows:

  24:astore_2
  25:aload_0
  26:invokevirtual #3                  //Method inside_finally: () V
  29:aload_2
  30:athrow< C6/>31:return

Astore_2 will save the thrown exception object to the second element of the local variable array. The following two lines of instruction are used to invoke the method in the finally block.

25:aload_0
26:invokevirtual #3                  //Method inside_finally: () V

Finally, the exception is thrown through the following instructions

29:aload_2
  30:athrow

In the last case, if the command (that is, the code in the catch block) between 11 and 17 bytes runs out of any type of exception, it jumps to the 24th byte to begin executing the finally code.

The code in this case, like the above, executes the code in the finally block directly. Let's take a look at the return statement in the try and catch .

The example code is as follows, and there is a return statement in the CATCH block:


public class Exceptiontest {public
    void  testexception () {

        try{
            inside_try ();

        }
        catch (Exception e) {
            inside_catch (e);
            The Return;//catch block has a return statement
        }
        finally{
            inside_finally ();
        }
    Functions that are called in a try block, catch block, and finally block are public
    void Inside_try () {   } public
    void Inside_catch (Exception e) {  }
    public void inside_finally () {}
}

Use the Javap-c exceptiontest command to view the byte code as follows:

As you can see from the bytecode, the return statement in the CATCH block is executed after the finally block, even if an exception occurs in the try block.

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.