Here you see try > But one thing is for sure, the contents of the finally block are executed before the return statement in the try, and if there is a return statement in the Finall statement block, it is returned directly from finally. This is also the reason why the return in finally is not recommended. Here are a few things to look at.
Situation one (there is no return in return,finally in try):
Public classTrytest { Public Static voidMain (string[] args) {System.out.println (test ()); } Private Static intTest () {intnum = 10; Try{System.out.println ("try"); returnnum + = 80; }Catch(Exception e) {System.out.println ("error"); }finally{ if(Num > 20) {System.out.println ("num> 20: "+num); } System.out.println ("Finally"); } returnnum; }}
The output results are as follows:
Trynum>20:90finally90
Analysis: apparently "return num + = 80" was split into "num = num+80" and "return num" two statements, the line executes the "num = num+80" statement in the try, saves it, and before "return num" executes in try, The statement in finally is executed before the 90 is returned.
Case two (both try and finally have return):
Public classTrytest { Public Static voidMain (string[] args) {System.out.println (test ()); } Private Static intTest () {intnum = 10; Try{System.out.println ("Try"); returnnum + = 80; }Catch(Exception e) {System.out.println ("Error"); }finally{ if(Num > 20) {System.out.println ("NUM>20:" + num);} System.out.println ("Finally"); return100; } }}
The output results are as follows:
trynum>20:90finally100
Analysis: The return statement in a try is also split, and finally the return statement executes before the return statement in the try, so that return in the try is "overwritten" and is no longer executed.
Execution of a return statement in a try Catch finally statement in Java (summary version)