Exception 類別和屬性Exception class and properties

Exception 類別是例外狀況所繼承的基底類別。The Exception class is the base class from which exceptions inherit. 例如 InvalidCastException 類別的階層如下:For example, the InvalidCastException class hierarchy is as follows:

Object
  Exception
    SystemException
      InvalidCastException

Exception 類別具有下列屬性,讓您更容易了解例外狀況。The Exception class has the following properties that help make understanding an exception easier.

屬性名稱Property Name 描述Description
Data IDictionary 會將任意資料保存在索引鍵/值組。An IDictionary that holds arbitrary data in key-value pairs.
HelpLink 可保留說明檔的 URL (或 URN),以提供有關例外狀況原因的廣泛資訊。Can hold a URL (or URN) to a help file that provides extensive information about the cause of an exception.
InnerException 您可以在例外狀況處理期間,使用此屬性來建立及保留一系列的例外狀況。This property can be used to create and preserve a series of exceptions during exception handling. 您可以使用此屬性來建立新的例外狀況,其中包含先前攔截例外狀況。You can use it to create a new exception that contains previously caught exceptions. InnerException 屬性中的第二個例外狀況可以擷取原始的例外狀況,讓程式碼可以處理第二個例外狀況,以檢視其他額外的資訊。The original exception can be captured by the second exception in the InnerException property, allowing code that handles the second exception to examine the additional information. 例如,假設您有一個方法會接收格式不正確的引數。For example, suppose you have a method that receives an argument that's improperly formatted. 此程式碼會嘗試讀取引數,但擲回例外狀況。The code tries to read the argument, but an exception is thrown. 此方法會攔截例外狀況並擲回 FormatExceptionThe method catches the exception and throws a FormatException. 為了改善呼叫端判斷所擲回例外狀況原因的能力,有時需要讓方法攔截 Helper 常式所擲回的例外狀況,再擲回更清楚指出所發生錯誤的例外狀況。To improve the caller's ability to determine the reason an exception is thrown, it is sometimes desirable for a method to catch an exception thrown by a helper routine and then throw an exception more indicative of the error that has occurred. 您可以建立全新且更有意義的例外狀況,其中的內部例外狀況參考可設定為原始例外狀況。A new and more meaningful exception can be created, where the inner exception reference can be set to the original exception. 這個更有意義的例外狀況接著會擲回給呼叫端。This more meaningful exception can then be thrown to the caller. 請注意,透過這項功能,您可以建立一系列的連結例外狀況,每個例外狀況後面接著先前擲回的例外狀況。Note that with this functionality, you can create a series of linked exceptions that ends with the exception that was thrown first.
Message 提供有關例外狀況原因的詳細資料。Provides details about the cause of an exception.
Source 取得或設定造成錯誤的應用程式或物件的名稱。Gets or sets the name of the application or the object that causes the error.
StackTrace 包含可用來判斷發生錯誤位置的堆疊追蹤。Contains a stack trace that can be used to determine where an error occurred. 堆疊追蹤包括原始程式檔名稱和程式行號 (若有偵錯資訊的話)。The stack trace includes the source file name and program line number if debugging information is available.

大部分繼承自 Exception 的類別不會實作其他成員或提供其他功能;它們只會繼承自 ExceptionMost of the classes that inherit from Exception do not implement additional members or provide additional functionality; they simply inherit from Exception. 因此,您可以在例外狀況類別階層架構、例外狀況名稱和例外狀況所包含的資訊中,找到例外狀況的最重要資訊。Therefore, the most important information for an exception can be found in the hierarchy of exception classes, the exception name, and the information contained in the exception.

建議只擲回及攔截衍生自 Exception 的物件,但您可以擲回任何衍生自 Object 類別的物件作為例外狀況。We recommend that you throw and catch only objects that derive from Exception, but you can throw any object that derives from the Object class as an exception. 請注意,並非所有語言都能擲回及攔截不是衍生自 Exception 的物件。Note that not all languages support throwing and catching objects that do not derive from Exception.

另請參閱See also