I will have a method that throws an exception, and not one that throws it. As in the example below. I seem to remember that Microsoft recommended it, but I can't remember where.
Using this technique, if you want to change the type of exception for any reason, you need to do it in only one place (for example, changing from ConfigurationException to ConfigurationErrorsException when upgrading from .NET 1.x to .NET 2.0).
You also respect the DRY principle by having a single copy of the code that creates the exception with its message and any other data included in the exception.
You obviously would not do this in trivial cases (for example, you would not replace throw new ArgumentNullException("myParamName") with throw BuildArgumentNullException("myParamName") )
private static Exception BuildSomeException(... parameters with info to include in the exception ...) { string message = String.Format(...); return new SomeException(message, ...); } ... throw BuildSomeException(...);
source share