使用標準例外狀況類型Using Standard Exception Types

本節說明架構所提供的標準例外狀況,以及其使用方式的詳細資料。This section describes the standard exceptions provided by the Framework and the details of their usage. 清單完全不是完整的。The list is by no means exhaustive. 如需其他架構例外狀況類型的使用方式,請參閱 .NET Framework 參考檔。Please refer to the .NET Framework reference documentation for usage of other Framework exception types.

Exception 和 SystemExceptionException and SystemException

X 不會擲回 System.ExceptionSystem.SystemExceptionX DO NOT throw System.Exception or System.SystemException.

X 不會攔截 framework 程式碼中的 System.ExceptionSystem.SystemException,除非您想要重新擲回。X DO NOT catch System.Exception or System.SystemException in framework code, unless you intend to rethrow.

X 避免攔截 System.ExceptionSystem.SystemException,但最上層的例外狀況處理常式除外。X AVOID catching System.Exception or System.SystemException, except in top-level exception handlers.

ApplicationExceptionApplicationException

X 不會擲回或衍生自 ApplicationExceptionX DO NOT throw or derive from ApplicationException.

InvalidOperationExceptionInvalidOperationException

如果物件處於不適當的狀態, ✓ DO會擲回 InvalidOperationException✓ DO throw an InvalidOperationException if the object is in an inappropriate state.

ArgumentException、System.argumentnullexception 和 ArgumentOutOfRangeExceptionArgumentException, ArgumentNullException, and ArgumentOutOfRangeException

如果將不正確的引數傳遞給成員,則會擲回 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.

會在擲回 ArgumentException的其中一個子類別時,設定 ParamName 屬性。✓ 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不允許可公開呼叫的 api 明確或隱含地擲回 NullReferenceExceptionAccessViolationExceptionIndexOutOfRangeExceptionX 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 不會明確地擲回 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.

在出現任意堆疊溢位的情況中,幾乎不可能撰寫會保持一致的 managed 程式碼。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 不會明確地擲回 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 不會明確地擲回 COMExceptionExecutionEngineExceptionSEHExceptionX 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. 的授權再版,從 Krzysztof Cwalina 和 Brad Abrams 撰寫,並在 2008 年 10 月 22 日由 Addison-Wesley Professional 出版,作為 Microsoft Windows Development Series 一部份的 Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, 2nd Edition 節錄。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