Tuesday, July 29, 2008

Derive from Exception not ApplicationException

A friend of mine Steve Gregson asked me the other day what exception class you should derive from when creating custom exceptions knowing that I would answer ApplicationException. I naturally told him ApplicationException, but I was wrong.

This post talks about this very issue with an excerpt from Jeffrey Richter and the reason...TargetInvocationException...or at least one of them. The original framework design was that applications would derive from ApplicationException and the CLR would derive from class Exception. This would then enable some code higher up the chain to distinguish the difference between an application exception over a CLR exception and handle it accordingly. But as a lot of framework classes didn't follow the ApplicationException pattern it renders this class useless as it is not possible to know where the exception came from (CLR or app) higher up the chain.

Best practices for handling exceptions can be found here.

In fact Krzysztof Cwalina's blog above co-authored this book:

I happen to own the above book and I just checked, page 197 where Jeffrey talks about not to use ApplicationException class. I highly recommend the above book by the way.

So there you have it, just forget ApplicationException ever existed...

No comments: