Export (0) Print
Expand All

Exception.InnerException Property

Gets the Exception instance that caused the current exception.

[Visual Basic]
Public ReadOnly Property InnerException As Exception
[C#]
public Exception InnerException {get;}
[C++]
public: __property Exception* get_InnerException();
[JScript]
public function get InnerException() : Exception;

Property Value

An instance of Exception that describes the error that caused the current exception. The InnerException property returns the same value as was passed into the constructor, or a null reference (Nothing in Visual Basic) if the inner exception value was not supplied to the constructor. This property is read-only.

Remarks

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 what causes the initial error.

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

Example

[Visual Basic, C#, C++] The following example demonstrates throwing and catching an exception that references an inner exception.

[Visual Basic] 
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

[C#] 
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);
         }
      }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

public __gc class MyAppException : public ApplicationException {
public:
   MyAppException (String* message) : ApplicationException (message) {}
   MyAppException (String* message, Exception* inner) : ApplicationException(message, inner) {}
};

public __gc class ExceptExample {
public:
   void ThrowInner () {
      throw new MyAppException(S"ExceptExample inner exception");
   }
   void CatchInner() {
      try {
         this->ThrowInner();
      } catch (Exception* e) {
         throw new MyAppException(S"Error caused by trying ThrowInner.", e);
      }
   }
};

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

[Visual Basic, C#, C++] This code has the following output:

[Visual Basic, C#, C++] In Main

catch block. Caught: Error caused by trying ThrowInner. Inner Exception is

MyAppException: ExceptExample inner exception at ExceptExample.ThrowInner() at

ExceptExample.CatchInner()

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Exception Class | Exception Members | System Namespace

Show:
© 2014 Microsoft