Hello, everybody. Today we're going to talk about a question that is occasionally asked in written examinations and interviews. If you don't know the principle in your work, it will lead to abuse.
As you probably know, try blocks are used to catch exceptions, catch blocks are used to handle exceptions captured by try blocks, and finally blocks are used to close resources. A try block can be followed by more than one catch block. If a catch block does not follow, it must follow a final block.
Conclusion 1: When the return statement is encountered in the try block, the final Statement block will be implement ed before the method returns, but the return value is not affected by the reassignment in the final block.
public class FinallyTest { public static void main(String[] args) throws Exception { int a = 10; int sum = throwException(a); System.out.println("Execute the return result sum:" + sum); } public static int throwException(int a) throws Exception{ int b = 20; int result = 0; try{ System.out.println("implement try Statement block"); result = a+b; return result; }catch(Exception e){ System.out.println("implement catch Statement block"); }finally{ System.out.println("implement finally Statement block"); result = 1; }return result;
} }
Conclusion 2: When a return or throw exception statement is encountered in the catch block, the final block will be executed before the method returns, but the return value is not affected by the reassignment in the final block.
public class FinallyTest { public static void main(String[] args) throws Exception { int a = 10; int sum = throwException(a); System.out.println("Execute the return result sum:" + sum); } public static int throwException(int a) throws Exception{ int b = 20; int result = 0; try{ System.out.println("implement try Statement block"); result = b / 0; return result; }catch(Exception e){ System.out.println("implement catch Statement block"); return result; }finally{ System.out.println("implement finally Statement block"); result = 1; } } }
Conclusion 3: If try, final statement has return, ignore the return value of try, and use the return value of final.
public class FinallyTest { public static void main(String[] args) throws Exception { int a = 10; int sum = throwException(a); System.out.println("Execute the return result sum:" + sum); } public static int throwException(int a) throws Exception{ int b = 20; int result = 0; try{ System.out.println("implement try Statement block"); result = a + b; return result; }catch(Exception e){ System.out.println("implement catch Statement block"); }finally{ System.out.println("implement finally Statement block"); result = 1; return result; } } }
Conclusion 4: An exception occurs in the final statement block, and the code after the exception in the final statement block will not be executed.
public class FinallyTest {
public static void main(String[] args) throws Exception {
int a = 10;
int sum = throwException(a);
System.out.println("Execute the return result sum:" + sum);
}
public static int throwException(int a) throws Exception{
int result = 30;
try{
System.out.println("implement try Statement block");
return result;
}catch(Exception e){
System.out.println("implement catch Statement block");
}finally{
int exception = b / 0;
System.out.println("implement finally Statement block");
result = 1;
}
return result;
}
}
Conclusion 5: An exception occurs in the try block and the final block, and the exception in the final block will conceal the exception in the try block.
public class FinallyTest { public static void main(String[] args) throws Exception { int a = 10; int sum = throwException(a); System.out.println("Execute the return result sum:" + sum); } public static int throwException(int a) throws Exception{ int result = 0; try{ System.out.println("implement try Statement block"); result = a / 0 ; return result; }catch(Exception e){ throw new Exception(e); }finally{ int[] arr = new int[1]; arr[2] = 3; System.out.println("implement finally Statement block"); } } }
Through the above five cases, we should have a clear understanding of the execution order of return in try block, catch block and final block. Let's summarize below.
1. When a return statement is encountered in a try block, the final statement block is executed before the method returns, but the return value is not affected by the reassignment in the final block.
2. When a return or throw exception statement is encountered in the catch block, the final statement block will be executed before the method returns, but the return value is not affected by the reassignment in the final block.
3. If try, there is return in the final statement, ignore the return of try, and use the return of final.
4. When an exception occurs in the final statement block, the code following the exception in the final statement block will not execute.
5:try {} block has an exception, and finally block has an exception, the exception in final block will cover up the exception in try block.
In addition, as for the principle of 1,2, which is not affected by final block, I would like to make a supplement. Why do we change the return value of try block in final block, and the result is not affected?
If there is a return in the try statement, the value of the local variable in the try statement block is returned. The detailed implementation process is as follows:
First, we save the return value in local variables in try fast.
(2) Then execute the jsr instruction and jump to the final statement.
(3) After the final statement is executed, the value stored in the local variable table before returning will not be valid if the return value in the final block is changed. (But in the third scenario above, finally brings its own return return value)
Thank you for your observation. If there are any mistakes, you are welcome to point out that we should learn together and make progress together.~
If you see my article and find something useful, please share the article with your friends. I will update more simple, high-quality technical articles in the future! ____________