Java exception Try do you have return,finally code in it?

Source: Internet
Author: User
Tags finally block

try{} has a return statement, then the code in the finally{} immediately following this try will not be executed, when executed, before or after the return?

Will certainly execute. The code for the finally{} block only contains a try{} block that encounters System.exit (0), and the statement that causes the Java virtual machine to exit directly will not execute.

When the program executes try{} encounters a return, the program executes the return statement, but does not return immediately-that is, to prepare all the return statement to do, that is, when it is going to return, but does not return, the execution of the process to execute the finally block, When the finally block executes, it returns directly the result of the return statement that was already prepared.

For example, we have the following procedures:

public class Test

{

publicstatic void Main (string[] args)

{

SYSTEM.OUT.PRINTLN (new test (). Test ());;

}

Staticint Test ()

{

int x = 1;

Try

{

RETURNX;

}

Finally

{

System.out.println ("Finally block execution:" + ++x);

}

}

}

The output at this time is:

Finally block execution: 2

1

See that the finally block in the above program has been executed, and the program executes the finally block when the x variable has been increased to 2. However, the test () method returned is still 1, which is determined by the return statement execution process, Java will execute the return statement first, all the things that need to be processed to finish first, need to return the value is ready, but before returning, The program flow goes to the execute finally block, but the change to the x variable in the finally block has no effect on the return value.

But if the finally block also contains a return statement, it is another matter, because the return statement in the finally block will also cause the method to return, for example, the program is the following form:

public class Test

{

publicstatic void Main (string[] args)

{

SYSTEM.OUT.PRINTLN (new test (). Test ());;

}

Staticint Test ()

{

int x = 1;

Try

{

returnx++;

}

Finally

{

System.out.println ("Finally block execution:" + ++x);

RETURNX;

}

}

}

The output at this time is:

Finally block execution: 3

3

As described, the program executes the returnx++, and the program executes the return statement, just waiting to return, at which point the value of X is already 2, but the return value prepared by the program is still 1. The program flow then goes to the finally block, at which point the program will add to x again, then x becomes 3, and because the finally block also has a return x, the statement, so the program will be directly returned by this statement, so the above test () method will return 3.

Java exception Try do you have return,finally code in it?

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.