Self-summary, what is insufficient or good plan, hope that we give correct, grateful!
Purpose: Try catch finaly in the order of execution, and everyone review it again.
Method: Debug to confirm the order of execution. (need to introduce junit packages)
Needless to say, Debug is the only standard for verifying the Order of code execution, haha ...
Test One:
New test class: Exceptiontest
Packagecom.core.test007;Importorg.junit.Test; Public classexceptiontest {@Test Public voidMain () {put (test007 ()); } Public inttest007 () {intA = 10; Put ("Normal"); Try{put ("Try"); //A = 1/0;Put ("Try return before"); returnA; }Catch(Exception e) {put ("Catch"); } finally{put ("Finally"); } put ("Method return before"); return0; } Public Static voidput (Object obj) {System.out.println (obj); }}
The printed information is
Normal Try Try return before finally10
Debug, execute the statement order is: 1) The statement of the try body, print "Try"
2) in the try body, print "Try return before"
3) Jump into finaly body, print "finaly"
4) Execute the return statement in the try body again
5) Jump directly out of the function body and return to the main function body, print 10
Test Two:
Put the above Exceptiontest class code in [int a = 1/0;] Before the comment is deleted, save the run again to print the information:
Normal Try Catch finally return before0
Debug, execute the statement order is: 1) The statement of the try body, print "Try"
2) EXECUTE statement [int a = 1/0;] , enter the catch body and print "catch"
3) Jump into finaly body, print "finaly"
5) Jump out of the finaly body, print "method return before"
6) Execute return 0
7) Return main function, print 0
Summary: 1. The finaly is executed under any circumstances.
2. When executing a statement in the try body that has an exception (the exception statement in this case is: [int a = 1/0;]) , all statements after the exception statement in the try body are no longer executed during this execution, but instead enter the catch statement.
Try catch finaly in Java sequential invocation order