Make exceptions the exception (rather than the rule)


Happy New Year to anyone reading this! I'm ashamed to realise that it is over a month since I last blogged anything. I've had plenty things that I've come across that I've thought "oh I must blog that" but it's amazing how things like Christmas shopping distract you!.

In helping customers debug problems there are certain things that come up again and again that create "pain points". The main ones that come to mind are: threads, locks, memory, exceptions and performance. At least those seem to be the main ones I get involved with.

With exceptions, people are often surprised to realise just how many exceptions are occurring in their application that they didn't know about.  My colleague Tess wrote a great blog on this recently so I'm not going to go into lots of detail here. However a good "entry point" for Microsoft's guidelines on exception handling is Handling and Throwning Exceptions. It's important to not use exceptions to control normal program flow. Exceptions really should do what they say on the box and only occur in exceptional circumstances. If you can check for a particular condition without throwing an exception then do so. Throwing exceptions is bad for performance and also makes it harder to debug an application that is in production as they will get in your way and make it harder for you to get to the real (and truly unexpected) problem.


Comments (1)

  1. Garry Trinder says:

    You must note that checking return code of every function is also bad for performance – so exceptions can offer performance BENEFIFS if used wisely !

Skip to main content