There is a return statement in try {}, so the code in finally {} following this try will not be executed. When will it be executed, before or after return ?,
This is an interview question. First, the code in finally {} will certainly be executed. As for whether it is before or after return,
According to the answer, it is executed after return. I don't think so. Baidu has a bit of it. Some say that it is before return, some say it is after return, and some execute in the middle of return. Then a small test is performed as follows:
Static void Main (string [] args) {// Test (); System. console. writeLine ("ended"); System. console. readLine () ;}/// <summary> /// Test /// </summary> /// <returns> </returns> private static int Test () {try {System. console. writeLine ("Good, start"); return ReturnInt () ;}catch (Exception exception Exception) {System. console. writeLine ("I am an exception"); throw;} finally {System. console. writeLine ("I am finally ");}} /// <summary> /// return number /// </summary> /// <returns> </returns> private static int ReturnInt () {System. console. writeLine ("I am return"); return 1 ;}
The test results are as follows:
So I think the code in finally {} is executed after return.
This test is rough. If you have other methods to prove it, please advise.