If there is a return statement in the catch, will the statement inside the finally be executed?
Executes and executes in the middle of the return statement
1 Public classtest{2 Public Static voidMain (string[] args) {3 System.out.println (GetInt ());4 }5 Public Static intgetInt () {6 intA = 10;7 Try{8System.out.println (1/0);9}Catch(Exception e) {Ten e.printstacktrace (); Onea=10; A returnA; -}finally{ -A = 100; the } - } - } -
After the exception, the program executes to the 12th sentence, a=10, and then executes the return statement, because there is a finally statement, so jump to the finnally statement, at this time a=100, and then the program jumps to the return statement in the catch, at this time a and restore 10, So the statement in finally executes and executes in the middle of the return.
If you modify the program to:
1 Public classExample2 {2 Public Static voidMain (string[] args)throwshanderexception {3 System.out.println (GetInt ());4 }5 6 Public Static intgetInt () {7 intA = 10;8 Try{9System.out.println (1/0);Ten}Catch(Exception e) { One e.printstacktrace (); Aa=10; - returnA; -}finally{ theA = 100; - returnA; - } + } -}
The value of execution result A is 100, and the statement that executes finally returns the
The finally of Java learning