【发布时间】:2012-01-24 19:23:21
【问题描述】:
我在 c# 中有一个自定义异常类:
public class InformationException : Exception {}
现在这当然行不通了,因为异常没有构造函数。
只是确保:我真的必须自己添加它们? :
public class InformationException : Exception
{
public InformationException() : base() {}
public InformationException(string message): base(message) {}
public InformationException(string message, Exception innerException) : base(message, innerException) {}
}
但是为了让课程真正有用,我必须添加文档:
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) {}
}
我现在必须重复:
public class ClientException : InformationException { }
public class BusinessRuleException : InformationException { }
public class InvisibleException : Exception { }
public class ProgrammerException : InvisibleException { }
C#in the last 3 years 中没有我错过的任何更改?这仍然是继承类的预期方式吗?
更新:哎呀,原来你还必须提供受保护的构造函数:
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) {}
}
【问题讨论】:
-
定义“这当然行不通”的意思,因为它当然行得通。它是否有用是另一回事。
-
@ChrisWue 是的,我想知道自 2009 年以来 C# 语言是否有任何发展。或者更具体地说,为了解决我的问题,是否有某种
<summary base><summary>语法 -
Alex Yakunin 写道:“我有一个更好的答案:FiXml。”。不确定它是否能解决您的具体问题,但它在概念上肯定比在源代码中复制 cmets 更干净。
标签: c# exception inheritance