100% Pure

accept no imitations
Everything here is my opinion. I do not speak for your employer.
October 2008
November 2008

2008-10-08 »

You don't need exceptions to enjoy try..finally

try {
    File f = new File(whatever);
    return f.read(buf);
} finally {
    f.close();
}

Of course, this isn't a perfect example, since there are easier ways to do it in most languages. In C#, you could use a "using" statement.
With a proper refcounting GC (like in Perl), it would close the file automatically and immediately when 'f' goes out of scope. And in C++, you could have a destructor on the local variable f.

But there are various situations where you want to do some arbitrary cleanup upon returning from your function, even though your function has multiple exit points, and that cleanup can be pretty complicated.

In the Linux kernel, you see a lot of "goto error" statements with an error cleanup at the bottom of the function, but it's kind of messy. try..finally obsoletes that.

Notice that I didn't necessarily have any exceptions being thrown above; try..finally means "clean up no matter how I return," and one of the reasons for returning is indeed that an exception was thrown, but it's not the only reason. In this case, I returned without using an explicit temporary variable, while also handling exceptions cleanly.

I'm CEO at Tailscale, where we make network problems disappear.

Why would you follow me on twitter? Use RSS.

apenwarr on gmail.com