暗示的 finally 块是否适用于除了 try-with-resources 之外的 try 语句?

huangapple go评论64阅读模式
英文:

Is implicit finally block being applied for try statements other than try-with-resources?

问题

只存在于所有try语句(try,try-finally,try-catch-finally)中还是只存在于try-with-resources中?

英文:

Does implicit finally block exist for all try statements (try, try-finally, try-catch-finally) or only for try-with-resources?

答案1

得分: 1

The closest thing I can think of like "implicit closing functionality" is finalization: Some classes may have a finalize method implemented that executes at some later time, whenever the object is garbage collected. That would have to have the close method called so not sure implicit is the right word. There was no Closeable interface to indicate things that needed closing before 1.7 and try-with-resources, there was no interface you could tag a class with to tell the JVM it needed closing. And that has nothing to do with any kind of try block.

The try block came first and is entirely explicit. Try-with-resources came along later, it addresses exception masking issues that can happen when using try-finally blocks. This is my explanation of why try-with-resources was created.

英文:

No. The closest thing i can think of like "implicit closing functionality" is finalization: Some classes may have a finalize method implemented that executes at some later time, whenever the object is garbage collected. That would have to have the close method called so not sure implicit is the right word. There was no Closeable interface to indicate things that needed closing before 1.7 and try-with-resources, there was no interface you could tag a class with to tell the jvm it needed closing. And that has nothing to do with any kind of try block.

The try block came first and is entirely explicit. Try-with-resources came along later, it addresses exception masking issues that can happen when using try-finally blocks. This is my explanation of why try-with-resources was created.

huangapple
  • 本文由 发表于 2023年5月17日 21:51:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定