This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.
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. We find that the finally block will not be executed before a try block is executed:
public class TryCatchTest { private static int total() { int i = 11; if (i == 11) { return i; } try {system.out.println (execute try); } finally {system.out.println (finally); } return 0; } public static void main(String[] args) {system.out.println (execute main: + total()); }}Copy the code
Output result:
Run main: 11Copy the code
2. Create an error before executing the try block:
public class TryCatchTest { private static int total() { return 1 / 0; Try {system.out. println(execute try); } finally {system.out.println (finally);} finally {system.out.println (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() {try {system.out.println (execute try); System.exit(0); } catch (Exception e) {system.out.println (execute catch); } finally {system.out.println (finally); } return 0; } public static void main(String[] args) {system.out.println (execute main: + total()); }}Copy the code
Output result:
Perform the tryCopy 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() {try {system.out.println (execute try); return 11; } finally {system.out.println (finally); }} public static void main(String[] args) {system.out.println (execute main: + total()); }}Copy the code
Output result:
Run the try command. Run finally. Run main: 11Copy 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() { int i = 0; Try {system.out. println(execute try: + I); return i; } finally { ++i; System.out.println(finally: + I); }} public static void main(String[] args) {system.out.println (execute main: + total()); }} The following command output is displayed: Try: 0 Finally: 1 Main: 0Copy 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() {try {system.out.println (execute try); return 1; 7} finally {8 system.out.println (execute finally); 9 return 2; 13 public static void main(String[] args) {14 system.out.println (execute main: + total()); 15} 16} Command output: Try. Finally. Main: 2Copy the code
Example 2:
1public class TryCatchTest { 2 3 private static int total() { 4 int i = 1; 5 try {6 system.out. println(execute try: + I); 7 return i; 8 } finally { 9 ++i; 10 System.out.println(finally: + I); 11 return i; 15 public static void main(String[] args) {16 system.out.println (execute main: + total()); 17} 18} Command output: Try: 1. Finally: 2. Main: 2Copy the code
Example 3:
public class TryCatchTest { private static int total() { int i = 1; Try {system.out. println(execute try: + I); } finally { ++i; System.out.println(finally: + I); } return i; } public static void main(String[] args) {system.out.println (execute main: + total()); }}Copy the code
Execution Result:
Run the try: 1 command. Finally: 2 Command. Run main: 2Copy 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.