捕捉和引发标准异常类型

下面的准则介绍 .NET Framework 所提供的某些最常用异常的最佳做法。

Exception 和 SystemException

不要引发 System.Exception 或 System.SystemException。

不要在框架代码中捕捉 System.Exception 或 System.SystemException,除非打算再次引发。

避免捕捉 System.Exception 或 System.SystemException,在顶级异常处理程序中除外。

ApplicationException

一定要从 T:System.Exception 类而不是 T:System.ApplicationException 类派生自定义异常。

最初考虑自定义异常应该从 ApplicationException 类派生;但是并未发现这样有很大意义。 有关更多信息,请参见处理异常的最佳做法

InvalidOperationException

如果处于不适当的状态,则引发 System.InvalidOperationException 异常。 如果没有向属性集或方法调用提供适当的对象当前状态,则应引发 System.InvalidOperationException。 例如,向已打开用于读取的 System.IO.FileStream 写入时,应引发 System.InvalidOperationException 异常。

一组相关对象的组合状态对于操作无效时,也应引发此异常。

ArgumentException、ArgumentNullException 和 ArgumentOutOfRangeException

如果向成员传递了错误的参数,则引发 System.ArgumentException 或其子类型之一。 如果适用,首选派生程度最高的异常类型。

下面的代码示例演示当参数为 null(在 Visual Basic 中为 Nothing)时引发异常。

If (anObject = Nothing) Then
    Throw New ArgumentNullException("anObject", "Specify a non-null argument.")
End If
if (anObject == null)
{
    throw new ArgumentNullException("anObject",
        "Specify a non-null argument.");
}
if (anObject == nullptr)
{
    throw gcnew ArgumentNullException("anObject",
        "Specify a non-null argument.");
}

在引发 System.ArgumentException 或其派生类型之一时,设置 System.ArgumentException.ParamName 属性。 此属性存储导致引发异常的参数的名称。 请注意,可以使用构造函数重载之一设置该属性。

使用属性 setter 的隐式值参数的名称的值。

下面的代码示例演示一个属性,该属性在调用方传递 null 参数时引发异常。

Public Property Address() As IPAddress
    Get
        Return IPaddr
    End Get
    Set(ByVal value As IPAddress)
        If IsNothing(value) Then
            Throw New ArgumentNullException("value")
        End If
        IPaddr = value
    End Set
End Property
public IPAddress Address
{
    get
    {
        return address;
    }
    set
    {
        if(value == null)
        {
            throw new ArgumentNullException("value");
        }
        address = value;
    }
}
property IPAddress^ Address
{
    IPAddress^ get()
    {
        return address;
    }
    void set(IPAddress^ value)
    {
        if (value == nullptr)
        {
            throw gcnew ArgumentNullException("value");
        }
        address = value;
    }
}

不要允许可公开调用的 API 显式或隐式引发 System.NullReferenceException、System.AccessViolationException、System.InvalidCastException 或 System.IndexOutOfRangeException。 应进行参数检查以避免引发这些异常。 引发这些异常会公开方法的实现细节,这些细节可能会随时间发生更改。

StackOverflowException

不要显式引发 System.StackOverflowException。 此异常仅应由公共语言运行时 (CLR) 显式引发。

不要捕捉 System.StackOverflowException。

以编程方式处理堆栈溢出极为困难。 应允许此异常终止进程并使用调试确定问题的根源。

OutOfMemoryException

不要显式引发 System.OutOfMemoryException。 此异常仅应由 CLR 基础结构引发。

ComException 和 SEHException

不要显式引发 System.Runtime.InteropServices.COMException 或 System.Runtime.InteropServices.SEHException。 这些异常仅应由 CLR 基础结构引发。

不要显式捕捉 System.Runtime.InteropServices.SEHException。

ExecutionEngineException

不要显式引发 System.ExecutionEngineException。

部分版权所有 2005 Microsoft Corporation。 保留所有权利。

部分版权所有 Addison-Wesley Corporation。 保留所有权利。

设计指引的详细信息,请参阅"框架设计准则: 公约、 成语和可重复使用的模式。网络图书馆"书 Krzysztof Cwalina 和布拉德 · 艾布拉姆斯,2005年艾迪生 - 韦斯利,发表。

请参见

概念

选择要引发的正确异常类型

其他资源

类库开发的设计准则

异常设计准则