Is the Java finally statement executed before or after the return?

Source: Internet
Author: User
Tags finally block

There are a lot of people on the Internet to discuss the exception capture mechanism in Java try...catch...finally The finally statement in the block is bound to be executed? A lot of people say no, of course their answer is right, after I have tried, there are at least two cases in which the finally statement will not be executed:

(1) The Try statement is not executed, as returned before the try statement, so that the finally statement does not execute, which also shows that the finally statement is executed by the necessary rather than sufficient condition is: the corresponding Try statement must be executed to.

(2) There is system.exit (0) in the try block, such a statement, System.exit (0), terminates the Java Virtual Machine JVM, even the JVM is stopped, all is finished, and the finally statement is not executed.

Of course, there are a lot of people who explore the relationship between the execution of the finally statement and return, it's confusing to know if the finally statement is executed before the return of the try or after? I am also a confused, I think their statement is not correct, I think it should be: The finally statement is executed after the return statement of the try, return returned before the execution. This kind of argument is a bit contradictory, perhaps I am not very clear, the following I give some of the results of their own experiments and examples to support, there are any questions to welcome the proposed.

The 1.finally statement is executed after the return statement executes before return returns.

public class FinallyTest1 {

public static void Main (string[] args) {

System.out.println (Test1 ());

}

public static int test1 () {

int B = 20;

try {

System.out.println ("try block");

return B + = 80;

}

catch (Exception e) {

System.out.println ("Catch block");

}

finally {

System.out.println ("finally block");

if (b > 25) {

System.out.println ("b>25, B =" + B);

}

}

return b;

}

}

The operating result is:

Try Block

Finally block

b>25, B = 100

100

Indicates that the return statement has executed and then executes the finally statement, but does not return directly, but waits until the finally statement is finished and returns the result.

If this example is not considered sufficient to illustrate the situation, then add an example to reinforce the conclusion that:

public class FinallyTest1 {

public static void Main (string[] args) {

System.out.println (test11 ());

}

public static String test11 () {

try {

System.out.println ("try block");

return test12 ();

} finally {

System.out.println ("finally block");

}

}

public static String test12 () {

SYSTEM.OUT.PRINTLN ("return statement");

Return "after Return";

}

}

The result of the operation is:

Try Block

Return statement

Finally block

After return

Indicates that the return statement in the try was executed first but did not return immediately, until the finally execution is finished

Here you might think: if there is a return statement in Finally, is it just return, and the return in the try cannot be returned? Look underneath.

A return statement in a 2.finally block overrides return in the try block.

public class FinallyTest2 {

public static void Main (string[] args) {

System.out.println (Test2 ());

}

public static int test2 () {

int B = 20;

try {

System.out.println ("try block");

return B + = 80;

} catch (Exception e) {

System.out.println ("Catch block");

} finally {

System.out.println ("finally block");

if (b > 25) {

System.out.println ("b>25, B =" + B);

}

return 200;

}

return b;

}

}

The operating result is:

Try Block

Finally block

b>25, B = 100

200

This means that the return in the finally returns directly, regardless of whether there is a return statement in the try, there is a small detail to note, finally plus return, finally the outside of the return B becomes an unreachable statement, That is, you can never be executed, so you need to comment out or the compiler will error.

Here you may think: if there is no return statement in Finally, but modified the value of B, then return in the try is the modified value or the original value? Look underneath.

3. If no return statement overrides the return value in the finally statement, the original return value may change or be unchanged because of the modification in the finally.

Test Case 1:

/**

* Java Learning Exchange QQ Group: 589809992 We'll learn java! together.

*/

public class FinallyTest3 {

public static void Main (string[] args) {

System.out.println (Test3 ());

}

public static int Test3 () {

int B = 20;

try {

System.out.println ("try block");

return B + = 80;

} catch (Exception e) {

System.out.println ("Catch block");

} finally {

System.out.println ("finally block");

if (b > 25) {

System.out.println ("b>25, B =" + B);

}

b = 150;

}

Return 2000;

}

}

The operating result is:

Try Block

Finally block

b>25, B = 100

100

Test Case 2:

Import java.util.*;

public class FinallyTest6

{

public static void Main (string[] args) {

System.out.println (Getmap (). Get ("KEY"). toString ());

}

public static map<string, string> Getmap () {

map<string, string> map = new hashmap<string, string> ();

Map.put ("KEY", "INIT");

try {

Map.put ("KEY", "TRY");

return map;

}

catch (Exception e) {

Map.put ("KEY", "CATCH");

}

finally {

Map.put ("KEY", "FINALLY");

Map = null;

}

return map;

}

}

The operating result is:

FINALLY

Why is the B = 150 in the finally in test Case 1, and the Map.put ("KEY", "finally") in test Case 2, which has no effect, and the map = null; This is the question of whether Java is a value or a pass-through, simply: there is only a pass-through value in Java, which is why map = null does not work. This also indicates that the return statement is a return statement in a try instead of a finally outside of return B; If you do not believe it, you can try it and return B; instead, return 294 will have no effect on the original result.
Here you may want to think: is not every time the return must be a try in the return statement? So finally the return B is not a bit of a function? Please look below.

A return statement in a 4.try block will not be executed in the case of an exception, so that it returns to what it looks like.

/**

* Java Learning Exchange QQ Group: 589809992 We'll learn java! together.

*/

public class FinallyTest4 {

public static void Main (string[] args) {

System.out.println (Test4 ());

}

public static int test4 () {

int B = 20;

try {

System.out.println ("try block");

b = b/0;

return B + = 80;

} catch (Exception e) {

b + = 15;

System.out.println ("Catch block");

} finally {

System.out.println ("finally block");

if (b > 25) {

System.out.println ("b>25, B =" + B);

}

B + = 50;

}

return 204;

}

}

The operating result is:

Try Block

Catch block

Finally block

b>25, B = 35

85

In this case, the return from the try is not executed until the 0 exception occurred before the return, but the catch statement that catches the exception and the final finally statement are executed, and the changes to B both affect the final return value, then return b; it works. Of course if you change the return B to return 300 or something, the last one is 300, which is beyond doubt.
There may be another question here: What if there is a return statement in the catch? Of course, only in the case of exceptions can be executed, then the finally before the return? Look underneath.

5. When an exception occurs, the return execution in the catch is exactly the same as the execution of return in the try when no exception occurs.

/**

* Java Learning Exchange QQ Group: 589809992 We'll learn java! together.

*/

public class FinallyTest5 {

public static void Main (string[] args) {

System.out.println (Test5 ());

}

public static int Test5 () {

int B = 20;

try {

System.out.println ("try block");

b = b/0;

return B + = 80;

} catch (Exception e) {

System.out.println ("Catch block");

Return B + = 15;

} finally {

System.out.println ("finally block");

if (b > 25) {

System.out.println ("b>25, B =" + B);

}

B + = 50;

}

return b;

}

}

The results of the operation are as follows:

Try Block

Catch block

Finally block

b>25, B = 35

35

After the exception has occurred, the return statement in the catch executes first, determines the return value, then executes the finally block, executes the catch and returns, and finally the change to B has no effect on the return value, the reason is the same as before. This means that the situation is exactly the same as the return statement in try.

Finally, the statement of a finally block is executed after the return statement in a try or catch is executed and the modified statement in finally may or may not affect the return value determined in the try or catch. If there is a return statement in Finally, the return statement in the try or catch is overwritten directly.

Source: CSDN

Is the Java finally statement executed before or after the return?

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.