One, foreword

Because I asked some students finally questions in this interview, I found that I did not remember clearly. Some points may have been misspoken by others, which made the scene a little awkward. So this article delves into finally execution and return values.

2. Will finally be executed?

Answer first: Definitely not.

We can look at two cases:

1. Return directly before executing the try block. We find that the finally block is not executed
public class TryCatchTest {

	private static int total(a) {
		int i = 11;
		if (i == 11) {
			return i;
		}
		try {
			System.out.println("Perform a try");
		} finally {
			System.out.println("Perform the finally");
		}
		return 0;
	}

	public static void main(String[] args) {
		System.out.println("Execute main:"+ total()); }} Output: Execute main:11
Copy the code
2. Make an error before executing the try block and go viral
public class TryCatchTest {

	private static int total(a) {
		return 1 / 0;
		try {
			System.out.println("Perform a try");  // This line is popular because it cannot be accessed
		} finally {
			System.out.println("Perform the finally");
		}
		return 0;
	}

	public static void main(String[] args) {
		System.out.println("Execute main:"+ total()); }}Copy the code

If a program cannot execute a try block, then the finally block will not execute.

But some of you might ask: If a try block is executed, must a finally block be executed? For some students, the answer is definitely yes. In fact, no. Take a look at the following examples:

public class TryCatchTest {

	private static int total(a) {
		try {
			System.out.println("Perform a try");
			System.exit(0);
		} catch (Exception e) {
			System.out.println("Executive catch");
		} finally {
			System.out.println("Perform the finally");
		}
		return 0;
	}

	public static void main(String[] args) {
		System.out.println("Execute main:"+ total()); }} Output: Executetry
Copy the code

We exit the JVM in the try block, and we’re done. Of course, this case is more extreme, remember, do not mess with this. To summarize, we find that the finally block still executes, whether we create an exception ina try block or return ina try block. Exception handling is designed to always execute the finally block. This summary is proven when finally is executed.

Finally, the implementation of the opportunity to explore

First, the general case:

public class TryCatchTest {

	private static int total(a) {
		try {
			System.out.println("Perform a try");
			return 11;
		} finally {
			System.out.println("Perform the finally"); }}public static void main(String[] args) {
		System.out.println("Execute main:"+ total()); }} Output: ExecutetryperformfinallyThe main implementation:11
Copy the code

In this example, the finally block is executed before the return of the try block. We create an exception in the try block:

public class TryCatchTest {

	private static int total(a) {
		try {
			System.out.println("Perform a try");
			return 1 / 0;
		} catch (Exception e) {
			System.out.println("Executive catch");
			return 11;
		} finally {
			System.out.println("Perform the finally"); }}public static void main(String[] args) {
		System.out.println("Execute main:"+ total()); }} Output: ExecutetryperformcatchperformfinallyThe main implementation:11
Copy the code

Similarly, finally is executed before the catch block return.

Return value in finally block

The 1.finally block does not return a value, but does change the value of a variable

Look at an example:

public class TryCatchTest {

	private static int total(a) {
		int i = 0;
		try {
			System.out.println("Execute try:" + i);
			return i;
		} finally {
			++i;
			System.out.println("Execute finally:"+ i); }}public static void main(String[] args) {
		System.out.println("Execute main:"+ total()); }} Output: Executetry:0performfinally:1The main implementation:0
Copy the code

If you look at the previous analysis, it’s not quite what you’d expect. In the first place, finally blocks should be executed before return. In the second place, finally blocks should be executed before return. In the second place, finally blocks should be executed before return. It’s going to return 1. The result, however, is 0. This is because Java programs reserve the return value ina try or catch block, temporarily confirming the return value before executing a statement ina finally block. After the finally block completes, if there is no return value in the finally block, the reserved return value is returned.

2. Finally contains a return value

Example 1:

public class TryCatchTest {

	private static int total(a) {
		try {
			System.out.println("Perform a try");
			return 1;
		} finally {
			System.out.println("Perform the finally");
			return 2; }}public static void main(String[] args) {
		System.out.println("Execute main:"+ total()); }} Output: ExecutetryperformfinallyThe main implementation:2
Copy the code

Example 2:

public class TryCatchTest {

	private static int total(a) {
		int i = 1;
		try {
			System.out.println("Execute try:" + i);
			return i;
		} finally {
			++i;
			System.out.println("Execute finally:" + i);
			returni; }}public static void main(String[] args) {
		System.out.println("Execute main:"+ total()); }} Output: Executetry:1performfinally:2The main implementation:2
Copy the code

Example 3:

public class TryCatchTest {

	private static int total(a) {
		int i = 1;
		try {
			System.out.println("Execute try:" + i);
		} finally {
			++i;
			System.out.println("Execute finally:" + i);
		}
		return i;
	}

	public static void main(String[] args) {
		System.out.println("Execute main:"+ total()); }} Result: The command is executedtry:1performfinally:2The main implementation:2
Copy the code

All three examples illustrate that when analyzing the return value of a method with a finally block, you need to analyze where the return appears. When a return operation is performed on a finally block, the return value of the method as a whole is the return value in the finally block. If a return occurs ina method after the finally block, the value of the return is the value of the above operation.