InnerException 属性
此文章由机器翻译。 将光标移到文章的句子上,以查看原文。 更多信息。

Exception.InnerException 属性

获取导致当前异常的 Exception 实例。

命名空间:  System
程序集:  mscorlib(在 mscorlib.dll 中)

public Exception InnerException { get; }


描述导致当前异常的错误的一个对象。 InnerException 属性返回的值与传递到 Exception(String, Exception) 构造函数中的值相同,如果没有向构造函数提供内部异常值,则为 null 此属性是只读的。



When an exception X is thrown as a direct result of a previous exception Y, the InnerException property of X should contain a reference to Y.

Use the InnerException property to obtain the set of exceptions that led to the current exception.

You can create a new exception that catches an earlier exception. The code that handles the second exception can make use of the additional information from the earlier exception to handle the error more appropriately.

Suppose that there is a function that reads a file and formats the data from that file. In this example, as the code tries to read the file, an IOException is thrown. The function catches the IOException and throws a FileNotFoundException. The IOException could be saved in the InnerException property of the FileNotFoundException, enabling the code that catches the FileNotFoundException to examine the cause of the initial error.

The InnerException property, which holds a reference to the inner exception, is set upon initialization of the exception object.

The following example demonstrates throwing and catching an exception that references an inner exception.

using System;

public class AppException : Exception
   public AppException(String message) : base (message)

   public AppException(String message, Exception inner) : base(message,inner) {}

public class Example
   public static void Main()
      Example ex = new Example();

      try {
      catch(AppException e) {
         Console.WriteLine ("In catch block of Main method.");
         Console.WriteLine("Caught: {0}", e.Message);
         if (e.InnerException != null)
            Console.WriteLine("Inner exception: {0}", e.InnerException);

   public void ThrowInner ()
      throw new AppException("Exception in ThrowInner method.");

   public void CatchInner() 
      try {
      catch (AppException e) {
         throw new AppException("Error in CatchInner caused by calling the ThrowInner method.", e);
// The example displays the following output:
//    In catch block of Main method.
//    Caught: Error in CatchInner caused by calling the ThrowInner method.
//    Inner exception: AppException: Exception in ThrowInner method.
//       at Example.CatchInner()

.NET Framework


.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

适用于 Windows Phone 应用的 .NET

受以下版本支持:Windows Phone 8.1、Windows Phone Silverlight 8



适用于 Windows 应用商店应用的 .NET


Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(支持带 SP1 或更高版本的服务器核心角色;不支持 Itanium)

.NET Framework 并非支持每个平台的所有版本。有关支持的版本的列表,请参阅 .NET Framework 系统要求


© 2015 Microsoft