Execution order of try, cache and finally in java

1. When there is return after try and finally, the statement and return in try will be executed first, then the statement and return in finally will be executed, and finally the return statement in finally will be returned.

2. In the same way, catch is the same as above 

3. When there is return after try but not in finally, the statement and return in try will be executed first, and the return result will be saved in the temporary domain (should be the function stack), then the statement in finally will be executed, and then return to return in try. It is worth noting that no matter how the return statement in the try is changed in the finally, it will not affect the return value in the final try.

4. In the same way, catch is the same as above

5. Regardless of whether there is an exception, the code in finally will be executed

6. It is best not to include return in the code in finally, because finally will return directly instead of returning the return value in try

7. Try must exist, catch and finally can not exist, but cannot exist at the same time

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325685306&siteId=291194637
Recommended