Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherit constructor documentation in C#?

i have an custom exception class in c#:

public class InformationException : Exception {}

Now of course this doesn't work, because the exception has no constructors.

Just making sure: i really have to add them myself? :

public class InformationException : Exception
{
    public InformationException() : base() {}
    public InformationException(string message): base(message) {}
    public InformationException(string message, Exception innerException) : base(message, innerException) {}
}

But then in order for the class to actually be useful i have to add the docs:

public class InformationException : Exception
{
    /// <summary>
    /// Initializes a new instance of the System.Exception class.
    /// </summary>
    public InformationException() : base() {}

    /// <summary>
    /// Initializes a new instance of the System.Exception class with a specified error message.
    /// </summary>
    /// <param name="message"> The message that describes the error.</param>
    public InformationException(string message): base(message) {}

    /// <summary>
    /// Initializes a new instance of the System.Exception class with a specified error message and a reference to the inner exception that is the cause of this exception.
    /// </summary>
    /// <param name="message"> The error message that explains the reason for the exception.</param>
    /// <param name="innerException">The exception that is the cause of the current exception, or a null reference 
    /// (Nothing in Visual Basic) if no inner exception is specified.</param>
    public InformationException(string message, Exception innerException) : base(message, innerException) {}
}

Which i now have to repeat for:

public class ClientException : InformationException { }

public class BusinessRuleException : InformationException { }

public class InvisibleException : Exception { }

public class ProgrammerException : InvisibleException { }

There hasn't been any changes in C# in the last 3 years that i missed? This is still the intended way to inherit classes?


Update: Whoops, turns out you also have to provide the protected constructor:

public class InformationException : Exception
{
    public InformationException() : base() {}
    public InformationException(string message): base(message) {}

    /// <summary>
    /// Initializes a new instance of the System.Exception class with serialized data.
    /// </summary>
    /// <param name="info">The System.Runtime.Serialization.SerializationInfo that holds the serialized
    /// object data about the exception being thrown.</param>
    /// <param name="context">The System.Runtime.Serialization.StreamingContext that contains contextual
    /// information about the source or destination.</param>
    /// <exception cref="System.ArgumentNullException">The info parameter is null</exception>
    /// <exception cref="System.Runtime.Serialization.SerializationException">The class name is null or System.Exception.HResult is zero (0).</exception>
    protected InformationException(SerializationInfo info, StreamingContext context);

    protected Exception(SerializationInfo info, StreamingContext context): base(info, context) {}
    public InformationException(string message, Exception innerException) : base(message, innerException) {}
}
like image 301
Ian Boyd Avatar asked Sep 16 '25 10:09

Ian Boyd


1 Answers

/// <inheritdoc />

seems to do the trick in Visual Studio 16.4, i.e.

/// <inheritdoc />
public ClientException(string message): base(message) {}

This only works for the parameters if they have the same name as the base constructor parameters, of course.

like image 194
EM0 Avatar answered Sep 18 '25 23:09

EM0