使用标准异常类型Using Standard Exception Types

本部分介绍 Framework 提供的标准异常及其用法的详细信息。This section describes the standard exceptions provided by the Framework and the details of their usage. 该列表并非详尽无遗。The list is by no means exhaustive. 有关其他 Framework 异常类型的用法,请参阅 .NET Framework 参考文档。Please refer to the .NET Framework reference documentation for usage of other Framework exception types.

Exception 和 SystemExceptionException and SystemException

X DO NOT 引发System.ExceptionSystem.SystemExceptionX DO NOT throw System.Exception or System.SystemException.

X DO NOT 捕获System.ExceptionSystem.SystemException在 framework 代码中,除非您想要重新引发。X DO NOT catch System.Exception or System.SystemException in framework code, unless you intend to rethrow.

X AVOID 捕捉System.ExceptionSystem.SystemException,除非在顶级异常处理程序。X AVOID catching System.Exception or System.SystemException, except in top-level exception handlers.

ApplicationExceptionApplicationException

X DO NOT 引发或从其派生ApplicationExceptionX DO NOT throw or derive from ApplicationException.

InvalidOperationExceptionInvalidOperationException

✓ DO 引发InvalidOperationException如果此对象处于不合适的状态。✓ DO throw an InvalidOperationException if the object is in an inappropriate state.

ArgumentException、ArgumentNullException 和 ArgumentOutOfRangeExceptionArgumentException, ArgumentNullException, and ArgumentOutOfRangeException

✓ DO 引发ArgumentException或如果错误的自变量传递给成员及其子类型之一。✓ DO throw ArgumentException or one of its subtypes if bad arguments are passed to a member. 如果适用,倾向于使用的派生程度最高的异常类型。Prefer the most derived exception type, if applicable.

✓ DO 设置ParamName属性时引发的子类之一ArgumentException✓ DO set the ParamName property when throwing one of the subclasses of ArgumentException.

此属性表示引发异常的参数的名称。This property represents the name of the parameter that caused the exception to be thrown. 请注意,可以使用一个构造函数重载来设置该属性。Note that the property can be set using one of the constructor overloads.

✓ DO 使用value属性 setter 的隐式值参数的名称。✓ DO use value for the name of the implicit value parameter of property setters.

NullReferenceException、IndexOutOfRangeException 和 AccessViolationExceptionNullReferenceException, IndexOutOfRangeException, and AccessViolationException

X DO NOT 允许公开可调用 Api 显式或隐式引发NullReferenceExceptionAccessViolationException,或IndexOutOfRangeExceptionX DO NOT allow publicly callable APIs to explicitly or implicitly throw NullReferenceException, AccessViolationException, or IndexOutOfRangeException. 这些例外情况是保留和引发的执行引擎和在大多数情况下指示 bug。These exceptions are reserved and thrown by the execution engine and in most cases indicate a bug.

应检查参数以避免引发这些异常。Do argument checking to avoid throwing these exceptions. 引发这些异常会暴露方法的实现细节,这些细节可能会随着时间而改变。Throwing these exceptions exposes implementation details of your method that might change over time.

StackOverflowExceptionStackOverflowException

X DO NOT 显式引发StackOverflowExceptionX DO NOT explicitly throw StackOverflowException. 应仅由 CLR 显式引发异常。The exception should be explicitly thrown only by the CLR.

X DO NOT 捕获StackOverflowExceptionX DO NOT catch StackOverflowException.

在存在任意堆栈溢出的情况下编写保持一致的托管代码几乎是不可能的。It is almost impossible to write managed code that remains consistent in the presence of arbitrary stack overflows. 通过使用探测功能将堆栈溢出移动到明确定义的位置而不是从任意堆栈溢出中退出,CLR 的非托管部分可以保持一致。The unmanaged parts of the CLR remain consistent by using probes to move stack overflows to well-defined places rather than by backing out from arbitrary stack overflows.

OutOfMemoryExceptionOutOfMemoryException

X DO NOT 显式引发OutOfMemoryExceptionX DO NOT explicitly throw OutOfMemoryException. 将仅由 CLR 基础结构引发此异常。This exception is to be thrown only by the CLR infrastructure.

ComException、SEHException 和 ExecutionEngineExceptionComException, SEHException, and ExecutionEngineException

X DO NOT 显式引发COMExceptionExecutionEngineException,和SEHExceptionX DO NOT explicitly throw COMException, ExecutionEngineException, and SEHException. 这些异常将仅由 CLR 基础结构引发。These exceptions are to be thrown only by the CLR infrastructure.

部分版权 © 2005, 2009 Microsoft Corporation。保留所有权利。Portions © 2005, 2009 Microsoft Corporation. All rights reserved.

经 Pearson Education, Inc 授权,转载自框架设计准则:可重用的 .NET 库的约定、习惯用语和模式,第 2 版 作者:Krzysztof Cwalina 和 Brad Abrams,由 Addison Wesley Professional 于 2008 年 10 月 22 日印发,作为 Microsoft Windows 开发系列的一部分。Reprinted by permission of Pearson Education, Inc. from Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, 2nd Edition by Krzysztof Cwalina and Brad Abrams, published Oct 22, 2008 by Addison-Wesley Professional as part of the Microsoft Windows Development Series.

请参阅See also