gpt4 book ai didi

c# - 我应该抛出 ArgumentException 吗?

转载 作者:太空宇宙 更新时间:2023-11-03 17:12:04 24 4
gpt4 key购买 nike

<分区>

这个问题是关于约定和解释 MSDN 的,所以我不认为它主要基于意见。

我想知道 ArgumentException :我有一个构建器类,用于构建一个过滤器对象,该对象将在从邮箱中检索一组电子邮件时应用。构建器有多种方法来设置过滤器选项。例如,我有两种设置过滤器“发送日期”范围的方法——在 XX 之前发送和/或在 XX 之后发送。我想为每个子句添加一个保护子句,如果提供的“之前”日期是提供的“之后”日期之后,这将引发异常。我会用一种常见的验证方法来做到这一点:

/// <summary>
/// This class provides methods for validating dates in various ways.
/// </summary>
internal static class DateValidation
{
/// <summary>
/// Validate the provided "start" and "end" date/time values.
/// If they do not represent a valid range, throw an exception.
/// </summary>
/// <param name="start">The date/time that represents the start of the range.</param>
/// <param name="end">The date/time that represents the end of the range.</param>
internal static void ValidateDateTimeRange(DateTime? start, DateTime? end)
{
if (start.HasValue && end.HasValue)
{
if (start.Value > end.Value)
throw new Exception(
string.Format(@"The start date/time ""{0}"" " +
@"occurs after the end date/time ""{1}"".",
start.ToString(), end.ToString()));
}
}
}

这是两个构建器方法:

/// <summary>
/// Set a value which represents a date/time after which
/// messages must have been sent to be part of filtered output.
/// </summary>
/// <param name="afterDateTime">The date/time after which
/// messages must have been sent to be part of filtered output.</param>
public void SetSentAfterDateTime(DateTime afterDateTime)
{
DateValidation.ValidateDateTimeRange(afterDateTime, _sentBeforeDateTime);
_sentAfterDateTime = afterDateTime;
}

/// <summary>
/// Set a value which represents a date/time before which
/// messages must have been sent to be part of filtered output.
/// </summary>
/// <param name="beforeDateTime">The date/time before which
/// messages must have been sent to be part of filtered output.</param>
public void SetSentBeforeDateTime(DateTime beforeDateTime)
{
DateValidation.ValidateDateTimeRange(_sentAfterDateTime, beforeDateTime);
_sentBeforeDateTime = beforeDateTime;
}

根据 MSDN :

Most commonly, an ArgumentException is thrown by the common language runtime or another class library and indicates developer error.

我知道“最常见”这个短语为其他用法留下了可能性,就像我的用法一样,但我喜欢坚持惯例。我正在构建一个公共(public) API,因此将记录异常并将在公共(public)接口(interface)之外冒泡;此外,它不会“指示开发人员错误”。它可以可以想象地指示用户错误(使用异常来处理用户输入验证问题是一种常见的约定)。不过,根据 MSDN 的描述,我感觉这不是它的目的。

...Derived classes [ArgumentNullException and ArgumentOutOfRangeException] should be used instead of ArgumentException, except in situations where neither of the derived classes is acceptable. For example, exceptions should be thrown by:

...

ArgumentOutOfRangeException when the value of an argument is outside the range of acceptable values; for example, when the value "46" is passed as the month argument during the creation of a DateTime.

我的论点可能超出了可接受值的范围,但该条件是根据其他日期/时间值动态确定的。没有“超出范围”的静态值范围。

ArgumentException 通常用于这种情况吗?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com