c# - 当属性不能为 null 时使用什么异常类型?

标签 c# .net exception

在我的应用程序中,如果特定类的属性为 null 或空(如果它是字符串),我需要抛出异常。我不确定在这种情况下使用什么是最好的异常(exception)。我不想创建一个新的异常,而且我不确定 ArgumentNullException 在这种情况下是否合适。

我应该创建一个新的异常(exception)还是有一个我可以使用的异常(exception)?

我不介意抛出 ApplicationException。

最佳答案

MSDN guidelines for standard exceptions状态:

Do use value for the name of the implicit value parameter of property setters.

The following code example shows a property that throws an exception if the caller passes a null argument.

public IPAddress Address
{
    get
    {
        return address;
    }
    set
    {
        if(value == null)
        {
            throw new ArgumentNullException("value");
        }
        address = value;
    }
}

另外,MSDN guidelines for property design说:

Avoid throwing exceptions from property getters.

Property getters should be simple operations without any preconditions. If a getter might throw an exception, consider redesigning the property to be a method. This recommendation does not apply to indexers. Indexers can throw exceptions because of invalid arguments.

It is valid and acceptable to throw exceptions from a property setter.

因此在 null 的 setter 中抛出 ArgumentNullException,在空字符串上抛出 ArgumentException,并且在 getter 中什么都不做。由于 setter 抛出并且只有您可以访问支持字段,因此很容易确保它不会包含无效值。让 getter throw 是没有意义的。然而,这可能是使用 Debug.Assert 的好地方.

如果您真的无法提供合适的默认值,那么我想您有以下三种选择:

  1. 只需返回属性中的任何内容,并将此行为记录为使用契约(Contract)的一部分。让调用者处理它。您可能还需要构造函数中的有效值。不过,这可能完全不适合您的应用程序。

  2. 通过方法替换属性:setter 方法在传递无效值时抛出异常,getter 方法在从未为属性分配有效值时抛出 InvalidOperationException

  3. 从 getter 中抛出 InvalidOperationException,因为您可以将“从未分配过的属性”视为无效状态。虽然您通常不应该从 getter 中抛出异常,但我认为这可能是产生异常的一个很好的理由。

如果您选择选项 2 或 3,您还应该包含一个 TryGet- 方法,该方法返回一个 bool 指示属性是否已设置为有效值,如果是则返回该值一个 out 参数。否则,您会强制调用者准备好处理 InvalidOperationException,除非他们之前自己设置了该属性,因此知道它不会抛出。比较 int.Parseint.TryParse

我建议将选项 2 与 TryGet 方法一起使用。它不违反任何准则,并对调用代码施加最低要求。


关于其他建议
ApplicationException 太笼统了。 ArgumentException 对于 null 来说有点太笼统了,但其他方面还不错。 MSDN docs again :

Do throw the most specific (the most derived) exception that is appropriate. For example, if a method receives a null (Nothing in Visual Basic) argument, it should throw System.ArgumentNullException instead of its base type System.ArgumentException.

事实上,您根本不应该使用 ApplicationException (docs):

Do derive custom exceptions from the T:System.Exception class rather than the T:System.ApplicationException class.

It was originally thought that custom exceptions should derive from the ApplicationException class; however, this has not been found to add significant value. For more information, see Best Practices for Handling Exceptions.

InvalidOperationException 不是针对方法或属性的参数无效的情况,而是针对整个操作 无效的情况(docs)。它不应该从 setter 中抛出:

Do throw a System.InvalidOperationException exception if in an inappropriate state. System.InvalidOperationException should be thrown if a property set or a method call is not appropriate given the object's current state. For example, writing to a System.IO.FileStream that has been opened for reading should throw a System.InvalidOperationException exception.

顺便说一句,InvalidOperationException 用于对象当前状态下操作无效的情况。如果操作对整个类始终无效,则应使用NotSupportedException。 .

关于c# - 当属性不能为 null 时使用什么异常类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1488322/

相关文章:

c# - 使用字典作为数据源绑定(bind)组合框

c# - 绘制裁剪图像时可以抗锯齿吗?

c# - Fixture 是否可以创建 N 个对象的列表?

c# - C Sharp 中的 'friend' 关键字等效于什么?

c# - 将 ObservableCollection 保存到文件 (.txt)

.net - 为 Xamarin 的 Mono Touch/Android 框架编译 AWS SDK (.NET)

Java 正则表达式错误 - 没有第 1 组

android - 异步查询处理程序中的异常处理

c# - 将 .NET Core 控制台应用程序部署到 Azure WebJobs 时加载 hostpolicy.dll 时出错

java - Android 中的 IllegalArgumentException