Excellent piece on exceptions

IanG on Tap: When to Catch Exceptions - the answer is, of course, not as often as you'd expect... Ian's talking about managed code as can be seen from his final comments about expecting to see many more finally blocks than catch blocks, of course, in a proper language ;) like C++ you don't need any try blocks at all as RAII will deal with all your resource management for you...


Of course in a proper language like C# you don't need to write lots of little wrapper classes to implement RAII, because you can use finally blocks to deal with all your resource management for you... ;-)

Actually, the using statement often gets rid of the need to write your finally blocks explicitly. Most resources the need explicit tidying implement IDisposable, so you can just do this:

using (Foo f = new Foo())
using (Bar b = new Bar())

The C# compiler will generate the finally block for you to call Dispose on both f and b, just like the C++ compiler would generate code to execute your destructors for you.


Yeah, I know, just couldn't resist a little dig ;)


Leave a comment