try…finally for multiple objects

Today I found another common mistake. Someone used more than one object and has to free them.

Instead of nested try…finally blocks it is easier and more readable to do the same with one try…finally block

This solution can still be improved. The first object can be created outside the try…finally block

Please remember that all variables that are freed in the finally block must be initialized before the try statement.

I guess that most of you know this practice for many years but there are still people around who don’t.

This entry was posted in Tips and Tricks and tagged . Bookmark the permalink.