August 17, 2004

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...


Share this entry: Email it! | bookmark it! | digg it! | reddit!

Posted by Len at August 17, 2004 07:45 AM | Comments (2) | Categories : Geek Speak
Comments

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())
{
f.DoStuff(b);
}

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.

Posted by: Ian Griffiths at August 18, 2004 03:28 PM

Ian,

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

Len

Posted by: Len Holgate at August 18, 2004 05:30 PM
Post a comment









Remember personal info?




Enter this code in the box below to prove that you're not some kind of automated spam robot...