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

前端 未结 8 2192
情歌与酒
情歌与酒 2021-02-15 10:14

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 11:08

    The finally block will always run if an uncaught exception is thrown, but the rest of the code in the method will be skipped.

    So if you put clean-up code after the finally block, it won't get called if there is an exception.

提交回复
热议问题