Is finally block really necessary for the clean up code (like closing streams)?

前端 未结 8 2537
小蘑菇
小蘑菇 2021-02-15 10:09

I am very confused as to why do I need to need to put the clean-up code like closing streams in a finally block.

I\'ve read that the code in finally<

8条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-15 10:49

    If an Exception occurs, ie, the try block is executed, then, rest assured, the finally block will also be executed. It's just a safeguard option instead of putting an unreliable assumption that the rest of the method will get executed.

提交回复
热议问题