gogoWebsite

Java exception: (Instance Analysis) Which part in try-catch-finally can be omitted? If the catch is returned, will it be executed finally? The order of execution of return?

Updated to 20 days ago

Article Directory

  • 1. Which part in try-catch-finally can be omitted?
  • 2. If the catch is returned, will it be executed finally?
  • 3. Under what circumstances will finally code block not be executed?
  • 4. What is the execution order of return?
    • (1) Basic data types
    • (2) Reference data type
    • (3) Summary

1. Which part in try-catch-finally can be omitted?

All of the following three situations are OK:
try-catch
try-finally
try-catch-finally
catch or finally can be omitted. catch and finally cannot be omitted at the same time.

2. If the catch is returned, will it be executed finally?

meeting.

(1) The function of finally is that no matter what happens, the code in finally will be executed.

(2) If you return in catch, you will also execute the finally code block before returning.

(3) And if the finally code block contains a return statement, the return in other places will be overwritten.

(4) For data of basic data type, changing the return value in finally block has no effect on the return value, but on the data of referenced data type.

Note:
Finally, it is not necessarily executed.

3. Under what circumstances will finally code block not be executed?

(1) No try code block was entered;

(2) () Forced exit from the program;

(3) The daemon thread is terminated;

For detailed analysis, see the blog:Summary of the situation where finally code blocks are not executed

4. What is the execution order of return?

Let's take a look at it through a few code examples.

(1) Basic data types

(1) The return in finally overwrites the return in try and returns result=1.

	public static int testTryCatch1() {
		int result = 0;
		try {
			result = 1;
			return result+1;
		} catch (Exception e) {
			return result;
		} finally {
			return result;
		}
	}//Return 1

(2) Before try to find the return, store the variable 1 of the basic data type in the stack, and finally change the result in the result, which will not affect the return value in try.

	public static int testTryCatch2() {
		int result = 0;
		try {
			result = 1;
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result=2;
		}
	}
	//Return 1

(3) The return in finally overwrites the return in catch.

	public static int testTryCatch5() {
		int result = 0;
		try {
			result = 1/0;
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result=2;
			return result;
		}
	}
	//Return 2

(4) The return in finally overwrites the return in try.

	public static int testTryCatch1() {
		int result = 0;
		try {
			result = 1;
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result=2;
			return result;
		}
	}
	//Return 2

(5) Before executing the return in catch, store the return value 0 in the stack, so finally the operation of result does not affect the return value, so return 0.

	public static int testTryCatch6() {
		int result = 0;
		try {
			result = 1/0;
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result=2;	
		}
	}
	//Return 0

(2) Reference data type

The following four methods return results are AB, AB, AB, and AB.

It can be seen that for reference data types, before return, the reference variable result is stored in the stack, so even if the reference variable is finally operated, the return value will be affected.

/**
	  * Refer to the execution of return in the data type try_finally
	  * @return
	  */
	public static StringBuilder testTryCatch3() {
		StringBuilder result = new StringBuilder("A");
		try {
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result.append("B");
		    return result;
		}
	}
	
	public static StringBuilder testTryCatch4() {
		StringBuilder result = new StringBuilder("A");
		try {
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result.append("B");
		}
	}
	/**
	  * Reference data type catch_finally execution of return
	  * @return
	  */
	
	public static StringBuilder testTryCatch7() {
		StringBuilder result = new StringBuilder("A");
		try {
			int i=5/0;
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result.append("B");
		    return result;
		}
	}
	
	public static StringBuilder testTryCatch8() {
		StringBuilder result = new StringBuilder("A");
		try {
			int i=5/0;
			return result;
		} catch (Exception e) {
			return result;
		} finally {
			result.append("B");
		}
	}
	

(3) Summary

(1) First of all, finally return will definitely override the return value in try or catch.

(2) The finally code block must be executed before return. For statements that do not have return in finally:
If the return value is a basic data type, changes to the return value in the finally block will not affect the return value. Because the content of the return value has been stored in the stack before return.
If the return value is a reference data type, changes to the return value in the finally block will affect the return value. Because the address of the referenced object has been stored in the stack before return, the change in the finally block to the value of the referenced object will affect the return value.