Exception.InnerException 属性

定义

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

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

属性值

Exception

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

实现

示例

以下示例演示如何引发和捕获引用内部异常的异常。

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.ThrowInner()
//       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.ThrowInner()
//       at Example.CatchInner()
open System

type AppException =
    inherit Exception
    
    new (message: string) = { inherit Exception(message) }

    new (message: string, inner) = { inherit Exception(message, inner) }

let throwInner () =
    raise (AppException "Exception in throwInner function.")
    ()

let catchInner () =
    try
        throwInner ()
    with :? AppException as e ->
        raise (AppException("Error in catchInner caused by calling the throwInner function.", e) )

[<EntryPoint>]
let main _ =
    try
        catchInner ()
    with :? AppException as e ->
        printfn "In with block of main function."
        printfn $"Caught: {e.Message}"
        if e.InnerException <> null then
            printfn $"Inner exception: {e.InnerException}"
    0
// The example displays the following output:
//    In with block of main function.
//    Caught: Error in catchInner caused by calling the throwInner function.
//    Inner exception: Example+AppException: Exception in throwInner function.
//       at Example.throwInner()
//       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.ThrowInner()
'       at Example.CatchInner()

注解

当将异常 X 作为上一个异常 Y的直接结果引发时, InnerException 该属性 X 应包含对 Y的引用。

使用 InnerException 属性获取导致当前异常的异常集。

可以创建新的异常来捕获早期异常。 处理第二个异常的代码可以使用早期异常中的其他信息来处理错误。

假设有一个函数读取文件并格式化该文件中的数据。 在此示例中,当代码尝试读取文件时,将引发一个 IOException 。 该函数捕获 IOException 并引发一个 FileNotFoundExceptionIOException该属性可以保存在该FileNotFoundException属性中InnerException,使代码能够捕获FileNotFoundException初始错误的原因。

InnerException 属性在初始化异常对象时设置对内部异常的引用。

适用于