Clean up code in finalize() or finally()?

前端 未结 9 1042
不知归路
不知归路 2020-12-19 04:16

I had the general view that clean up of resources is done in the finally block,
recently I found this particular code snippet in a class and it was overridi

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 04:49

    They're not related. This is like asking, "Should you create objects in an initializer or in normal methods?" Like, it depends on what you're doing with the objects. A finalizer cleans up an object's state while it's destroyed (maybe — it's not something you should rely on), while a finally block executes code after a try block. There isn't any common situation where you'd be able to choose one or the other since they do different things.

提交回复
热议问题