Try/catch has return, finally also has return, Sun also said in finallyCodeRequired,
If return in finally is executed, will return in try/catch still be executed?
If all the return statements are executed, what is the order of execution? What is the value after the return statement actually returned?
To try:
Bytes ------------------------------------------------------------------------------------------------------
Public class main {
Public static void main (string... ARGs ){
System. Out. println (TCF ());
}
Public static int Tcf (){
Try {
System. Out. println ("try ");
Throw new exception ();
}
Catch (exception e ){
System. Out. println ("catch ");
Return one ();// Note the output in the One Method
}
Finally {
System. Out. println ("finally ");
Return two ();
}
}
Public static int one (){
System. Out. println ("one ");
Return 1;
}
Public static int two (){
System. Out. println ("two ");
Return 2;
}
}
Bytes -----------------------------------------------------------------------------------
The following result is displayed.
Bytes -----------------------------------------------------------------------------------
Try
Catch
One
Finally
Two
2
Bytes -----------------------------------------------------------------------------------
Conclusion:
First, return one (); is executed, and then the Finally block is executed according to the syntax, so return two () is executed ();
If you have learned how to compile, you can understand "return one ();" as compiled into several lines:
1 returnvalue = one ();
2 If (no finally statement block followed)
3Where goto calls this method;
4 else
5Execute finally statement Blocks
After two return statements are executed, it is equivalent to assigning two values to returnvalue, so returnvalue is equal to the last value.
What are the return values and output results of the following code? If you understand what I mean, you can easily draw a conclusion.
Bytes -----------------------------------------------------------------------------------
Try {
Try {
Return one ();
} Finally {
Return two ();
}
} Finally {
Return three ();
}