Exception.InnerException Exception.InnerException Exception.InnerException Exception.InnerException Property

定义

获取导致当前异常的 Exception 实例。Gets the Exception instance that caused the current exception.

public:
 property Exception ^ InnerException { Exception ^ get(); };
public Exception InnerException { get; }
member this.InnerException : Exception
Public ReadOnly Property InnerException As Exception

属性值

描述导致当前异常的错误的一个对象。An object that describes the error that caused the current exception. InnerException 属性返回的值与传递到 Exception(String, Exception) 构造函数中的值相同,如果没有向构造函数提供内部异常值,则为 nullThe InnerException property returns the same value as was passed into the Exception(String, Exception) constructor, or null if the inner exception value was not supplied to the constructor. 此属性是只读的。This property is read-only.

实现

示例

下面的示例演示如何引发和捕获引用内部异常的异常。The following example demonstrates throwing and catching an exception that references an inner exception.

using namespace System;

public ref class AppException: public Exception
{
public:
   AppException(String^ message ) : Exception(message)
   {}

   AppException(String^ message, Exception^ inner) : Exception(message, inner)
   {}
};

public ref class Example
{
public:
   void ThrowInner()
   {
      throw gcnew AppException("Exception in ThrowInner method.");
   }

   void CatchInner()
   {
      try {
         this->ThrowInner();
      }
      catch (AppException^ e) {
         throw gcnew AppException("Error in CatchInner caused by calling the ThrowInner method.", e);
      }
   }
};

int main()
{
   Example^ ex = gcnew Example();
   try {
      ex->CatchInner();
   }
   catch (AppException^ e) {
      Console::WriteLine("In catch block of Main method.");
      Console::WriteLine("Caught: {0}", e->Message);
      if (e->InnerException != nullptr)
         Console::WriteLine("Inner exception: {0}", e->InnerException);
   }
}
// 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()
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 {
         ex.CatchInner();
      }
      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 {
         this.ThrowInner();
      }
      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()
Public Class AppException : Inherits Exception
   Public Sub New(message As String)
      MyBase.New(message)
   End Sub
   
   Public Sub New(message As String, inner As Exception)
      MyBase.New(message, inner)
   End Sub
End Class

Public Class Example
   Public Shared Sub Main()
      Dim testInstance As New Example()
      Try
         testInstance.CatchInner()
      Catch e As AppException
         Console.WriteLine ("In catch block of Main method.")
         Console.WriteLine("Caught: {0}", e.Message)
         If e.InnerException IsNot Nothing Then
            Console.WriteLine("Inner exception: {0}", e.InnerException)
         End If
      End Try
   End Sub
   
   Public Sub ThrowInner()
      Throw New AppException("Exception in ThrowInner method.")
   End Sub
   
   Public Sub CatchInner()
      Try
         Me.ThrowInner()
      Catch e As AppException
         Throw New AppException("Error in CatchInner caused by calling the ThrowInner method.", e)
      End Try
   End Sub
End Class
' 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()

注解

X异常作为上一个异常Y的直接结果引发时, InnerException的属性X应包含对的引用YWhen 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.

使用 InnerException 属性获取导致当前异常的异常集。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. 在此示例中,当代码尝试读取文件IOException时,将引发。In this example, as the code tries to read the file, an IOException is thrown. 函数捕获IOExceptionFileNotFoundException引发。The function catches the IOException and throws a FileNotFoundException. 可以保存FileNotFoundException在的FileNotFoundException属性中,并启用捕获的代码来检查出现初始错误的原因。 InnerException IOExceptionThe 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.

InnerException属性(包含对内部异常的引用)是在初始化异常对象时设置的。The InnerException property, which holds a reference to the inner exception, is set upon initialization of the exception object.

适用于