I have learned the exception mechanism in the past two days, of which try...catch...finally is an important part in my opinion. Relating to the statements I learned before, I encountered the interesting problem of return and finally. After experiments, I found that the computer language This logical relationship is indeed subtle. The following is my own opinion. If there is something wrong, I hope you seniors can point it out:
First look at the first piece of code
public class return_finally{ public static void main(String[] args){ System.out.println( m_1() ); } public static int m_1(){ int i=10; try{ System.out.println( "start" ); return i+=10; }catch(Exception e){ System.out.println( "error: "+e ); }finally{ if(i>10){ System.out.println( i ); } System.out.println( "finally" ); } return i; } }
The output of the first piece of code is as follows:
start
20
finally
20
Note that the second return is outside the finally statement at this time. According to the regulations that return and finally appear at the same time, we can understand that the first return is just a condition. Its function is only to find the finally statement. In fact, it only executes an i+= 10 operation, then directly enter the finally statement, and finally return the result.
Let’s look at the second piece of code:
public class return_finally{ public static void main(String[] args){ System.out.println( m_1() ); } public static int m_1(){ int i=10; try{ System.out.println( "start" ); return i+=10; }catch(Exception e){ System.out.println( "error: "+e ); }finally{ if(i>10){ System.out.println( i ); } System.out.println( "finally" ); return 50; } } }
The difference between the second piece of code and the first piece is that the final return is placed in the finally statement. We can guess the output result:
start
20
finally
50
The return executed at this time has overwritten the 20 obtained from the previous operation result and returned the value 50. That is to say, the return in the finally statement has been executed. Well, it should be said like this~~
There is also a third piece of code, please enjoy it slowly:
public class return_finally{ public static void main(String[] args){ System.out.println( m_1() ); } public static int m_1(){ int i=10; try{ System.out.println( "start" ); return i; }catch(Exception e){ System.out.println( "error: "+e ); }finally{ if(i>10){ System.out.println( i ); } System.out.println( "finally" ); i=50; } return i; }}
At this time, there is an extra i=50 in the finally statement, so what is the result?
start
finally
10
This is the result. There is no return statement in the finally statement, so the original return value will not change.
So through these three examples, can we understand it this way:
When encountering the return in the try statement, first store the value in a place, and then look for the finally statement. If there is a new algorithm in the statement, take out the value from that space and perform the operation. If there is a return in finally, just put the "new value" "Overwrite the "old value" in that space and eventually return; if there is no return in finally, just take out the "old value" in that space and return it.
The above is purely for understanding, I hope you can give me more advice, thank you for your help!