异常类和属性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. 若要提高调用方确定引发异常的原因的能力,有时最好用一个方法捕获帮助器例程引发的异常,然后引发一个更能说明已发生错误的异常。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 的大多数类无法实现其他成员或提供其他功能;它们只从 Exception 进行继承。Most 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