- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
<分区>
这个问题是关于约定和解释 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
通常用于这种情况吗?
我有一个存储过程,我正在尝试使用 Dapper 执行该过程,该过程引发了一个错误,该错误似乎与我正在尝试做的事情无关,尽管我似乎无法弄清楚我在做什么错误的。 这是我试图调用的存储过程的签名: ALTE
我想匹配浏览器游戏中的坐标。我的正则表达式是: try { Regex r = new Regex("Mond ([1-9]) \x5B([1-
任何人都可以向我解释为什么我从这段代码中收到 ArgumentException 吗?它告诉我 Task.WhenAll 函数中的 tasks 参数包含空值。但是,它只是在我运行它时偶尔发生一次。 s
我正在尝试为 Eddystone 信标做广告,但我的代码在 advertisementData.Data 处失败并出现 ArgumentException: Value does not fall w
我已经编写了一些代码来从我的 Blogger 博客中导入内容。下载完所有 HTML 内容后,我将浏览图像标签并下载相应的图像。在大量情况下,System.Drawing.Bitmap.FromStre
我正在尝试更加熟悉表达式树。 我创建了一个简单的 switch 表达式,如下所示: var paramExp = Expression.Parameter(typeof(int)); Expressi
我正在尝试使用任务并行库逐个单元地构建矩阵。 我有以下代码可以执行此操作: List campaigns = GetHomeCampaigns(); Dictionary sellers = GetH
我试图通过添加三个参数来调用存储过程,但在添加第二个参数时出现以下 ArgumentException The SqlParameter is already contained by another
因为 CultureInfo 没有从一个线程复制到另一个线程,所以我使用了以下方法来为我做这件事。 public static StartCustomTask(Action action, TaskC
在我的代码中,我怎样才能显示弹出的警告框?我试图重播抛出新的参数异常并添加 Response.Write("alert('NOT Successful');");但它表明我在附近有语法错误”)”。如果
我有一个通过串行端口连接到设备的实用程序。为了测试这个应用程序,我正在为该设备编写一个模拟器。 这两个应用程序旨在在同一台 Windows 7 机器上运行。 我正在用 C# 编写模拟器。我使用 com
据我所知,ArgumentExceptions 通常是这样使用的: public void UpdateUser(User user) { if (user == null) throw ne
我有一个采用三个参数(3D 点)的方法。如果这些点是共线的,我想抛出一个异常。对我来说明显的异常(exception)是 ArgumentException,但最好的做法是在构造函数中包含参数名称。在
我正在使用 Path.Combine , 并且其中一个字符串包含 Unicode 字符。我收到 {System.ArgumentException} exception; illegal charac
当我尝试合并两个字典时,我不知道如何将键和值保留在字典中。由于 key 重复,我不断收到 ArgumentException。当键匹配时,我只想通过 =+ kvp.value; 添加值 我有一个字典列
当构造一个 ArgumentException 时,一些重载采用一个字符串作为无效参数的参数名称。我认为每当我更改方法的参数名称时不必记住更新此 ctor 参数会很好。有没有一种简单的方法可以使用反射
当我尝试合并两个字典时,我不知道如何将键和值保留在字典中。由于 key 重复,我不断收到 ArgumentException。当键匹配时,我只想通过 =+ kvp.value; 添加值 我有一个字典列
我想使用XamarinUITest在Xamarin上测试IOS应用程序。 不幸的是,我无法在本地设备上启动任何东西。 我已成功在Android上进行测试,但IOS似乎是一个全新的谜。 其实我有一个错误
我们的 SonarQube 经常在我们的代码中提出以下问题(代码异味):“用于 ArgumentException 的参数名称应与现有参数名称匹配”。 Here是触发此问题的规则。 触发此问题的示例如
在安装 WebActivatorEx nuget 包后尝试访问页面时,我不断收到以下 ArgumentException。 Server Error in '/PharmaDotnet/ux' App
我是一名优秀的程序员,十分优秀!