Why use Finally in Try … Catch

前端 未结 14 1997
鱼传尺愫
鱼传尺愫 2020-12-05 06:38

I see that the Finally in Try .. Catch will always execute after any parts of the execution of the try catch block.

Is it any different to

14条回答
  •  天命终不由人
    2020-12-05 06:49

    After reading the reply to my comment above I came to think of a few things.

    The question can basically not be answered completely with out knowing the code in question.

    The reason being that not all code can be put in a finally block. E.g. yield statements are not allow in finally (and catch) blocks. The try block might have several execution braches where some returns and some don't. The finally being executed in all those case whereas in the example with no finally that would not be the case for the clean up code. Further more you can't jump (goto) into a finally block though very uncommon you can jump to the code after the catch block. You can't return from a finally block either.

    There's quite a few although most very uncommon cases where the answer depends on the actual code.

提交回复
热议问题