> With RAII you don't have those problems because cleanup is
> defined once per class, not each time the resource is
> used. If Java had RAII you could spare 95% of all
> try/catch/finally blocks in the code. Lack of RAII is
> responsible for most of Java's proverbial verbosity.
I'm not going to say RAII isn't useful but there are other approaches to address the issue. I've written a library that addresses this issue in Java for databases.
A database query might look something like this:
Query query = Manager.getQuery("select bar from foo");
query.execute(new Handler() {
public void handle(Row row)
{
// do things with row
}
});
The resource cleanup is handled by the manager.