What is the point of finally in a try catch/except finally statement

后端 未结 4 546
清酒与你
清酒与你 2021-01-30 15:33

I have used try-catch/except-finally variants in many languages for years, today someone asked me what is the point of finally and I couldn\'t answer.

Basically why wo

4条回答
  •  有刺的猬
    2021-01-30 16:25

    Finally block is executed even if an exception thrown in the try block. Therefore, for instance if you opened a stream before, you may want to close that stream either an exception is thrown or not. Finally block is useful for such an issue.

提交回复
热议问题