Dolittle Documentation

DL0006

ExceptionConstructorParametersShouldNotContainMessage

Exception constructors should not suggest a generic use. Typically if the constructor takes a string which is supposed to be the message that gets presented together with the exception, this moves the responsibility for the message to the code that is throwing the exception and also opens up for generic use. This rule together with DL0005 is designed to avoid this.

Below is an example of good use:

/// <summary>
/// Exception that is thrown when a target is not alive in a weak reference.
/// </summary>
public class CannotInvokeMethodBecauseTargetIsNotAlive : ArgumentException
{
    /// <summary>
    /// Initializes a new instance of the <see cref="CannotInvokeMethodBecauseTargetIsNotAlive"/> class.
    /// </summary>
    /// <param name="method"><see cref="MethodInfo"/> for the method that can't be invoked.</param>
    public CannotInvokeMethodBecauseTargetIsNotAlive(MethodInfo method)
        : base($"Method '{method}' can't be invoked, since target has been collected by the garbage collector")
    {
    }
}

Notice that the parameter for the constructor is of type MethodInfo and we use the details from the parameter within the string that is now owned by the exception itself.