Bob Congdon has a post up on the difficulties of cleaning up external resources in Java if exceptions get thrown while they are in use. He points to a C# feature that ensures that such resources will be finalized, the using statement:
using (TextWriter tw = File.CreateText("log.txt")) {
tw.WriteLine("Foo");
// Other stuff
}
Apparently, the compiler expands that. In Smalltalk, we don't need a language feature for that - we have the #ensure: method. It looks like this:
stream := 'someFile' asFilename writeStream.
[stream nextPutAll: 'Foo'.
"other code here"]
ensure: [stream ifNotNil: [stream close]].
#ensure takes a block (closure) as an argument, and will execute regardless - if there are exceptions inside the block it's sent to or not. It's a very clean way to make sure that necessary code runs, when you aren't particularly concerned with the exact nature of any exceptions that pop up.