Return and finally execute a big debate on the impact of return value

The statement in the finally block is executed after the return statement in try or catch is executed before returning, and the modification statement in finally may or may not affect the return value determined by return in try or catch; if there is also a return statement in finally, it will overwrite try Or the return statement in the catch returns directly.

review

return: used to jump out of the current method, pointing to the return value pointer (pointer, pointer, pointer...).
finally: It will definitely be executed. Typically used to close resources.

The steps of the function return: then execute the finally statement block, and then return the pointer position of return. (So ​​far the execution of the function ends) So the position of the return pointer is fixed, which is an unchangeable fact. Any behavior that can change the content pointed by the position of the return pointer can change the return value.
先记录return指针位置,即此时返回值指针位置。


There are only the following two situations, nothing else! ! !

In fact, this is a question about value passing

Case 1: Change the location pointed to by the return value variable

The location of the returned pointer is immutable.
This behavior only changes the pointer position of the return value variable returnVal;

public static String compute(String args) {
    
    
	String returnVal = "init block " + args;
	try {
    
    
		returnVal = "try block " + args;
		int i = 1 / 0;
		return returnVal;
	} catch (Exception e) {
    
    
		returnVal = "catch block " + args;
		return returnVal;
	} finally {
    
    
		returnVal = "finally block " + args;
		System.out.println("finally block 执行了哦...");
	}
}

public static void main(String[] args) {
    
    
	System.out.println(compute("你好世界。"));
}

Case 2: Change the content of the location pointed to by the return value variable

The location of the returned pointer is immutable.
This behavior is to change the content of the pointer position of the return value variable list;

public static List<String> compute(String args) {
    
    
	List<String> list = new ArrayList<String>();
	list.add("init block " + args);
	try {
    
    
		list.add("try block " + args);
		int i = 1 / 0;
		return list;
	} catch (Exception e) {
    
    
		list.add("catch block " + args);
		return list;
	} finally {
    
    
		list.add("finally block " + args);
		System.out.println("finally block 执行了哦...");
	}
}

public static void main(String[] args) {
    
    
	System.out.println(compute("你好世界。"));
}

postscript

In fact, I came to verify the execution order of spring @AfterReturning, @AfterThrowing, and @Afterthree notifications, and I somehow remembered the problem of value transmission .

@After: It will be executed under any circumstances (corresponding to the notification in the finally block in the spring source code).
@AfterReturning: Execute after the pointcut method executes the return value.
@AfterThrowing: Executed after an exception is thrown.

Guess you like

Origin blog.csdn.net/Michael_lcf/article/details/122263860
Recommended