3

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) {}
}
Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • Define the meaning of "of course this doesn't work," because of course it does. Whether or not it is *useful* is another matter. – Anthony Pegram Dec 20 '11 at 21:17
  • Almost an exact duplicate: http://stackoverflow.com/questions/426484/why-are-constructors-not-inherited – ChrisWue Dec 20 '11 at 21:39
  • @ChrisWue Yes, i was wondering if there has been any developments in the C# language since 2009. Or more specifically, to solve my problem, is there some sort of `` syntax – Ian Boyd Dec 20 '11 at 22:22
  • Alex Yakunin wrote: "I have a better answer: FiXml.". Not sure if it solves your specific problem, but it's certainly conceptually cleaner than duplicating the comments in the source. – CodesInChaos Dec 20 '11 at 23:15

2 Answers2

2
/// <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.

EM0
  • 5,369
  • 7
  • 51
  • 85
-1

As described in this answer you can use GhostDoc

Community
  • 1
  • 1
aqwert
  • 10,559
  • 2
  • 41
  • 61