Issue
JEP 421, being released in the forthcoming Java 18, deprecates finalization. I understand this mean the finalize()
method is being deprecated. However, it also mentions the try/finally
block and mentions try-with-resources as an alternative, so I'm confused - is it saying that try/finally will be deprecated? Should we start changing our legacy code to replace try/finally with try-with-resources?
I thought that this JEP was only about the finalize()
method, but some pages on the internet (such as https://metebalci.com/blog/what-is-new-in-java-18/) are saying that try/finally is being deprecated, which sounds a bit alarming.
Solution
You should certainly prefer try-with-resources when you can, since it is more concise and avoids the possibility of forgetting to close an autocloseable resource, but no, finally
is not going away.
The JEP mentions finally
in a context where it is used incorrectly. Besides using a try-with-resources construct, you could nest two try/finally blocks to fix the error, as the JEP notes: "The fix here involves a nested try-finally construct, and is left as an exercise for the reader."
Answered By - David Conrad
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.