Directly on the code instance:
public static void Main (string[] args) {
System.out.println (Test1 ());
}
static int test1 () {
int a=0;
try {
return Ret1 ();
} catch (Exception e) {
Todo:handle exception
}finally {
return Ret2 ();
}
}
static int Ret1 () {
System.out.println ("Return1");
return 1;
}
static int Ret2 () {
System.out.println ("return2");
return 2;
}
Output Result:
Return1
Return2
2
Results analysis: When the Main method is executed, reversed to the Test1 () method, no exception occurs, executes the contents of the try, call the Ret1 () method because there is return, so jump directly. After execution of the contents of the try is done, because there is no exception so do not go catch, execute finally inside the method, the last return returned to execute test1. Returns to the main method. All output results.
In Java, try Catch finally executes