In depth analysis of the execution sequence of try catch finally

  • 2020-04-01 02:16:12
  • OfStack

Try is executed first, and if catch is executed with an exception, then finally is executed anyway

A function must execute a part in finally.

The way a function executes is, when there's a return, the function stores this data in some place, and then it tells the main function, I'm not going to execute it, you're going to execute it, so the function comes out.

But when a function in the finally, finally never to perform, so, even if the try and catch has been carried out in return, but this function will not quit, don't tell the main function to perform, but waiting for the finally performed to go back to tell the main function to perform, if finally appeared in return, the return value will overwrite the try and catch the values, then we will tell the main function: I don't perform, you go to perform. So the main function gets the return value in finally. That is, the return value of the subfunction is always the return value in finally (as long as there is a value in the called function)

Related articles: