1, try with return
Scenario 1: (when the parameter is a basic type)
private int testReturn1() { 2 int i = 1; 3 try { 4 i++; 5 System.out.println("try:" + i); 6 return i; 7 } catch (Exception e) { 8 i++; 9 System.out.println("catch:" + i); 10 } finally { 11 i++; 12 System.out.println("finally:" + i); 13 } 14 return i; 15 }
Output:
try:2
finally:3
2
Because when a try contains a return, it will first execute the code before return, then temporarily save the information that needs to be returned, then execute the code in finally, and finally return the previously saved information through return. Therefore, the value returned by the method here is 2 calculated in try, not 3 calculated in finally
Scenario 2: (parameter is reference type)
private List<Integer> testReturn2() { 2 List<Integer> list = new ArrayList<>(); 3 try { 4 list.add(1); 5 System.out.println("try:" + list); 6 return list; 7 } catch (Exception e) { 8 list.add(2); 9 System.out.println("catch:" + list); 10 } finally { 11 list.add(3); 12 System.out.println("finally:" + list); 13 } 14 return list; 15 }
Output:
try:[1]
finally:[1, 3]
[1, 3]
After reading this example, you may find a problem. When you just mentioned return, you will temporarily save the information to be returned, which is not affected by finally. Why are there changes here? In fact, the problem lies in the parameter type. The last example used the basic type, and the reference type used here. What is stored in the list is not the variable itself, but the address of the variable. Therefore, when finally changes the variable through the address, it will still affect the return value of the method.
2, catch with return
private int testReturn3() { 2 int i = 1; 3 try { 4 i++; 5 System.out.println("try:" + i); 6 int x = i / 0 ; 7 } catch (Exception e) { 8 i++; 9 System.out.println("catch:" + i); 10 return i; 11 } finally { 12 i++; 13 System.out.println("finally:" + i); 14 } 15 return i; 16 }
Output:
try:2
catch:3
finally:4
3
Return in catch is the same as that in try. First execute the code before return, then temporarily save the information to return, then execute the code in finally, and finally return the previously saved information through return. Therefore, the value returned by the method here is 3 after the cumulative calculation in try and catch, rather than 4 after the calculation in finally.
3, finally with return
private int testReturn4() { 2 int i = 1; 3 try { 4 i++; 5 System.out.println("try:" + i); 6 return i; 7 } catch (Exception e) { 8 i++; 9 System.out.println("catch:" + i); 10 return i; 11 } finally { 12 i++; 13 System.out.println("finally:" + i); 14 return i; 15 } 16 }
Output:
try:2
finally:3
3
When there is a return in finally, the return in try will become invalid. After the final return is executed, the return in try will not be executed again. In this way, the compiler can compile, but the compiler will give a warning, so it is not recommended to write return in finally, which will destroy the integrity of the program, and once an exception occurs in finally, it will cause the exception in catch to be overwritten.
Summary:
1. finally, the code in is always executed.
2. When there is a return in try and catch, finally will also be executed. When returning, pay attention to whether the type of return value is affected by the code in finally.
3. When there is a return in finally, it will exit directly in finally, resulting in the invalidation of the return in try and catch.