我的自定义exception是否应该inheritance与它们类似的exception,或者只是从exceptioninheritance?

我在我的应用程序中创建了一些自定义exception。

如果我有一个在测试参数状态后抛出的exception,或者我有一个在测试int在适当范围内后抛出的exception,我的exception是否会inheritanceArgumentException和IndexOutOfRangeException,还是应该inheritanceException?

由于inheritance用于指定要捕获的exception,因此您应该在做出决定时尊重这一点。

可以想象一个带有附加信息的IOException,或ArgumentOutOfRangeException或ArgumentNullException以外的ArgumentException。

假设你真的需要一个自定义exception,我会从exception中inheritance你最喜欢的东西,而不仅仅是Exception。

也就是说,我发现,在大多数情况下,使用正确的措辞,Exception消息通常足以创建一个全新的exception。

例如,如何throw new IntOutOfProperRangeException();throw new ArgumentOutOfRangeException("The int value was too large?");显着不同throw new ArgumentOutOfRangeException("The int value was too large?");

我认为创建一个新的Exception类型总是更安全。 如果您需要更改其处理方式,将更容易找到您正在或可能正在处理它的情况。 找到MyException要比查找ArgumentOutOfRangeException的特定情况容易得多。 您似乎能够在exception中提供一些额外的信息,并且创建exception并不是太多工作。

此外,我倾向于inheritance像MyBaseException这样的基本应用程序类,并确保为exception添加XML注释。

我只是好奇,你为什么不实际使用已经存在的exception? 听起来这些exception正是您所需要的,为什么反对使用它们?

就个人而言,如果我有一个索引器并且索引值超出范围,那么我只是抛出现有的IndexOutOfRangeException,我不会去inheritance它的麻烦。

如果您只讨论相似但不完全相同的exception,那么请看一下框架中提供的模式。 看起来这看起来不合理,inheritance描述了“is-a”关系。

如果您不需要向exception添加任何其他数据,那么我只使用像IndexOutOfRangeException这样的本机.NETexception。

但是,如果您需要将某些内容与您的exception相关联,而您无法使用IndexOutOfRangeException进行本机操作,那么我将inheritance它。 这里的好处是您可以捕获新的自定义exception类型或IndexOutOfRangeException。 当然,如果您捕获基本类型,则不会有额外的属性等。

恕我直言,从另一个exceptioninheritance是没有问题的。 它使该例外的目的更加清晰。 但请确保适用于ParentException的所有内容也适用于您创建的ChildException。 否则,你最终会遇到“Square extends Rectangle”问题 ……

几乎总是我使用IllegalArgumentException(nulls和/或超出范围的值)和IllegalStateException用于不比IOException,SQLException,Null更具体的任何东西……

如果您只使用通用Exception,则永远无法捕获为您的应用程序定制的特定exception。 如果你只是使用

 try { } catch (Exception ex) { } 

您将捕获每个exception,而无法过滤特定错误。

我创建自定义exception的另一个原因是处理可能由于多种原因而发生的特定于应用程序的exception。 这允许意味着抛出自定义exception,但自定义与exception关联的消息。 它还为我的特定应用程序提供了另一级别的error handling。

例如,我有一个适用于皮带传动系统尺寸的工程应用。 该DLL也可供其他人使用。 我有一个应用程序exception,当选择中发生错误时抛出该exception。 出错的原因可能有很多(驱动速度无效,马力要求不正确等)。 由于失败的原因很多,因此自定义应用程序exception允许我提供失败的具体详细信息。

这也允许我向用户记录方法调用将抛出他们需要处理的特定于应用程序的exception。

如果inheritanceException类,请确保在基类中实现具有消息,消息+内部exception和序列化exception的构造函数。

这是我的一个例子。

 ///  /// Drive error exception class. Thrown when a drive selection error has occured. ///  [Serializable] public class DriveException : ApplicationException { ///  /// Default constructor. ///  public DriveException() { } ///  /// Constructor used with a message. ///  /// String message of exception. public DriveException(string message) : base(message) { } ///  /// Constructor used with a message and an inner exception. ///  /// String message of exception. /// Reference to inner exception. public DriveException(string message, Exception inner) : base(message, inner) { } ///  /// Constructor used in serializing the data. ///  /// Data stored to serialize/de-serialize /// Defines the source/destinantion of the straeam. public DriveException(SerializationInfo info, StreamingContext context) : base(info, context) { } } 

我认为这一切都取决于你是否想要将ArgumentNotInPersitableStateexception作为ArgumentOutOfRange捕获。 如果会有这样一个catch块(或者你正在编写一个框架,其他人可能会使用它),那么你应该inheritance相关的exception类型。