Jan 6, 2009

A More Structured Approach to Exceptions

Exception management.  Ah, the two words that can so easily send shivers up the backs of many developers.  And they are what they are, right?  Wrap things in a try/catch and go from there.

Well, Michael Feathers has just posted a blog entry on a slightly different approach to exceptions for those times where you want to be able to call a function and then handle any exceptions in a more structured way than just doing some inline processing (or swallowing them whole!).

Go have a look, it’s an interesting and simple approach and you could easily convert it to an extension method if you wanted to make your code just that little bit more intuitive.

1 comment:

  1. That's an interesting approach, but I'm used to the regular syntax so this one at first seems odd and it will be hard for devs to use it.

    That said, I've used extension methods to avoid checks for null and encapsulate repetitive code. For example instead of the common way or raising an event we could do something like:

    MyEvent.Raise(myEventArgs);

    or if it simply uses EventArgs then don't default it to EventArgs.Empty.

    Internally the extension method will check if someone is subscribed.

    Something similar with actions, with an extension method that checks that the function isn't null before calling it.

    Action.SafeCall();

    ReplyDelete