Exception.InnerException 属性

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

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

语法

声明
Public ReadOnly Property InnerException As Exception
用法
Dim instance As Exception
Dim value As Exception

value = instance.InnerException
public Exception InnerException { get; }
public:
virtual property Exception^ InnerException {
    Exception^ get () sealed;
}
/** @property */
public final Exception get_InnerException ()
public final function get InnerException () : Exception

属性值

一个 Exception 实例,它描述导致当前异常的错误。InnerException 属性返回与传递给构造函数的值相同的值,或者,如果没有向构造函数提供内部异常值,则返回空引用(Visual Basic 中为 Nothing)。此属性为只读。

备注

当异常 X 作为以前的异常 Y 的直接结果发生时,X 的 InnerException 属性应当包含对 Y 的引用。

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

您可以创建对以前的异常进行捕捉的新异常。处理第二个异常的代码可利用前一个异常的其他信息更适当地处理错误。

假定有一个可以读取文件并格式化该文件中数据的函数。在此示例中,当代码试图读取文件时引发 IOException。该函数捕捉 IOException 并引发 FileNotFoundExceptionIOException 可以保存在 FileNotFoundExceptionInnerException 属性中,从而使捕捉 FileNotFoundException 的代码可以检查导致初始错误的原因。

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

示例

下面的示例演示引发和捕捉引用内部异常的异常。

Imports System

Public Class MyAppException
   Inherits ApplicationException
   
   Public Sub New(message As [String])
      MyBase.New(message)
   End Sub 'New
   
   Public Sub New(message As [String], inner As Exception)
      MyBase.New(message, inner)
   End Sub 'New
End Class 'MyAppException

Public Class ExceptExample
   
   Public Sub ThrowInner()
      Throw New MyAppException("ExceptExample inner exception")
   End Sub 'ThrowInner
   
   Public Sub CatchInner()
      Try
         Me.ThrowInner()
      Catch e As Exception
         Throw New MyAppException("Error caused by trying ThrowInner.", e)
      End Try
   End Sub 'CatchInner
End Class 'ExceptExample

Public Class Test
   
   Public Shared Sub Main()
      Dim testInstance As New ExceptExample()
      Try
         testInstance.CatchInner()
      Catch e As Exception
         Console.WriteLine("In Main catch block. Caught: {0}", e.Message)
         Console.WriteLine("Inner Exception is {0}", e.InnerException)
      End Try
   End Sub 'Main
End Class 'Test
using System;
public class MyAppException:ApplicationException 
{
   public MyAppException (String message) : base (message) 
   {}
   public MyAppException (String message, Exception inner) : base(message,inner) {} 
   }
public class ExceptExample 
{
   public void ThrowInner () 
   {
   throw new MyAppException("ExceptExample inner exception");
   }
   public void CatchInner() 
   {
      try 
      {
      this.ThrowInner();
      }
         catch (Exception e) 
         {
         throw new MyAppException("Error caused by trying ThrowInner.",e);
         }
      }
   }
public class Test 
{
   public static void Main() 
   {
   ExceptExample testInstance = new ExceptExample();
      try 
      {
      testInstance.CatchInner();
      }
         catch(Exception e) 
         {
         Console.WriteLine ("In Main catch block. Caught: {0}", e.Message);
         Console.WriteLine ("Inner Exception is {0}",e.InnerException);
         }
      }
}
using namespace System;
public ref class MyAppException: public ApplicationException
{
public:
   MyAppException( String^ message )
      : ApplicationException( message )
   {}

   MyAppException( String^ message, Exception^ inner )
      : ApplicationException( message, inner )
   {}

};

public ref class ExceptExample
{
public:
   void ThrowInner()
   {
      throw gcnew MyAppException( "ExceptExample inner exception" );
   }

   void CatchInner()
   {
      try
      {
         this->ThrowInner();
      }
      catch ( Exception^ e ) 
      {
         throw gcnew MyAppException( "Error caused by trying ThrowInner.",e );
      }

   }

};

int main()
{
   ExceptExample^ testInstance = gcnew ExceptExample;
   try
   {
      testInstance->CatchInner();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "In Main catch block. Caught: {0}", e->Message );
      Console::WriteLine( "Inner Exception is {0}", e->InnerException );
   }

}
import System.*;
public class MyAppException extends ApplicationException
{
    public MyAppException(String message)
    {
      super(message);
    } //MyAppException

    public MyAppException(String message, System.Exception inner)
    {
      super(message, inner);
    } //MyAppException
} //MyAppException

public class ExceptExample
{
    public void ThrowInner()throws MyAppException
    {
        throw new MyAppException("ExceptExample inner exception");
    } //ThrowInner

    public void CatchInner()throws MyAppException
    {
        try {
            this.ThrowInner();
        }
        catch (System.Exception e) {
            throw new MyAppException("Error caused by trying ThrowInner.", e);
        }
    } //CatchInner
} //ExceptExample

public class Test
{
    public static void main(String[] args)
    {
        ExceptExample testInstance = new ExceptExample();
        try {
            testInstance.CatchInner();
        }
        catch (System.Exception e) {
            Console.WriteLine("In main catch block. Caught: {0}", 
                e.get_Message());
            Console.WriteLine("Inner Exception is {0}", 
                e.get_InnerException());
        }
    } //main
} //Test

此代码的输出如下:

In Main
  catch block. Caught: Error caused by trying ThrowInner. Inner Exception is
  MyAppException: ExceptExample inner exception at ExceptExample.ThrowInner() at
  ExceptExample.CatchInner()

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Exception 类
Exception 成员
System 命名空间