Tuesday, June 15, 2004

More on Try/Finally.

Along the lines of my previous post, this month's MSDN Magazine has an interesting article about unexpected errors in managed applications. On the "Some Best Practices" topic, Jason puts some quite strong arguments that go exactly along the lines of my post.
Some highlights:

* Don't catch or throw base exception types
* Use finally blocks liberally

(...)

Now let's look at the guideline surrounding finally blocks. In general, well-written managed code contains more try/finally constructs than it does try/catch constructs. The reason for this is that finally blocks guarantee the ability to perform method-level clean-up operations in both exceptional and non-exceptional executions. Many methods include code that qualifies as "clean-up operations." This includes the closing of files and other resources that were opened in the method, unlocking synchronization locks that were taken during the method, and so forth. Meanwhile, if you follow the first guideline-only catching expected exceptions-the number of methods that actually catch any exceptions at all is reduced in the end.

It's nice to see that I'm not alone thinking this way!

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?