英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论