PackageCom.swift; Public classTry_catch_finally_test { Public Static voidMain (string[] args) {/** There is a return statement in try{}, then the code in the finally {} immediately after this try will not be executed, when executed, before or after the return? */System.out.println ("Execute return does not return a value, the value is staged, waiting for finally to finish executing and then return the saved value" +Newtry_catch_finally_test (). Test ()); } Static intTest () {intx = 1; Try { return++x; } finally { ++x; System.out.println ("Finally executes first, return finally executes" +x); } }}
Finally there is a return, and finally returns in the finally
PackageCom.swift; Public classTry_catch_finally_test { Public Static voidMain (string[] args) {/** There is a return statement in try{}, then the code in the finally {} immediately after this try will not be executed, when executed, before or after the return? */System.out.println ("Execute return does not return a value, the value is staged, waiting for finally to finish executing and then return the saved value" +Newtry_catch_finally_test (). Test ()); } Static intTest () {intx = 1; Try { return++x; }Catch(Exception e) {e.printstacktrace (); return++x; }finally { ++x; System.out.println ("Finally executes first, return finally executes" +x); return++x; } }}
Some say that return executes 2 times and finally executes in the middle, which can be understood and may also be convenient rote
But I think that return is executed after finally, after finally execution returns the return value to the call, only to notice that the returned value is not the final value, but that return will return the return value but is temporarily saved because finally appears unable to return the value. Here's a break.
Java Basic interview question: try{} has a return statement, then the last {} immediately after the try code will be executed, when executed, before or after the return?