RAII, AC/DC, and the "with" statement
blog.brush.co.nz
blog.brush.co.nz
In general, it is wise to allocate only one unmanaged (non-garbage collected) resource per constructor.
File.open("foo.txt", "w") do |f|
f.write("this is a test\n")
endFirst couple of hits for "Java RAII" on Google suggest "no," but I think the HN audience might be smarter than Google about this. I just recently introduced (then found and fixed) a bug where I forgot to release a resource, and if there is an idiom that doesn't make me manually match every resource allocation with its inverse, I'd like to know about it. Looks like Java might be last to the "automated freeing of non-memory resources" party.
And though it doesn't specifically say so in the article, this technique really needs to be applied everywhere in a C++ program and its libraries. Maybe your program doesn't throw exceptions, but something else might; if your cleanup isn't automatic, it may not always occur.