Last week I replied to a post about exceptions, it made me think those programming .Net daily take for granted the etiquette of using Exceptions. So I thought I would share some of my thoughts… well it is a sort of a rules’ish list.
- Exceptions can be expensive, so avoid using them for normal conditions
- Only catch the exceptions you can handle
- Don’t hide/swallow exceptions
- Don’t catch System.Exception as will also catch unmanaged exceptions such as System.Runtime.InteropServices.SEHException
- Consider using your own custom exceptions or derive them from similar ones
- Remember inner exceptions when processing an exception
- API’s such as MethodInfo.Invoke throw TargetInvocationException which contains the real exception
- Use the Exception suffix on your custom exception
- Consider using Microsoft’s StyleCop to point out common issues
- Avoid using System.ApplicationException if you want to use the code in the Silverlight CLR
- Remember to serialize your own exception types
- Use xml comment docs for the exceptions a method raises… it helps intellisense..
I suspect I might have missed something.. so feel free to comment..