Exception.InnerException Property

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Gets the Exception instance that caused the current exception.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

Public ReadOnly Property InnerException As Exception
public Exception InnerException { get; }

Property Value

Type: System..::.Exception
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.

Examples

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

Public Class Example

    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
        Dim testInstance As New ExceptExample()
        Try
            testInstance.CatchInner()
        Catch e As Exception
            outputBlock.Text += String.Format("In Main catch block. Caught: {0}", e.Message) & vbCrLf
            outputBlock.Text += String.Format("Inner Exception is {0}", e.InnerException) & vbCrLf
        End Try
    End Sub
End Class
Public Class MyException
    Inherits ArgumentException

    Public Sub New(ByVal message As [String])
        MyBase.New(message)
    End Sub

    Public Sub New(ByVal message As [String], ByVal inner As Exception)
        MyBase.New(message, inner)
    End Sub
End Class

Public Class ExceptExample

    Public Sub ThrowInner()
        Throw New MyException("MyException inner exception")
    End Sub

    Public Sub CatchInner()
        Try
            Me.ThrowInner()
        Catch e As Exception
            Throw New MyException("Error caused by trying ThrowInner.", e)
        End Try
    End Sub
End Class
using System;
public class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        ExceptExample testInstance = new ExceptExample();
        try
        {
            testInstance.CatchInner();
        }
        catch (Exception e)
        {
            outputBlock.Text += String.Format("Exceptino caught: {0}", e.Message) + "\n";
            outputBlock.Text += String.Format("Inner Exception is {0}", e.InnerException) + "\n";
        }
    }
}

public class MyException : ArgumentException
{
    public MyException(String message)
        : base(message)
    { }
    public MyException(String message, Exception inner) : base(message, inner) { }
}
public class ExceptExample
{
    public void ThrowInner()
    {
        throw new MyException("MyException inner exception");
    }
    public void CatchInner()
    {
        try
        {
            this.ThrowInner();
        }
        catch (Exception e)
        {
            throw new MyException("Error caused by trying ThrowInner.", e);
        }
    }
}

This code has the following output:

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

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

Exception Class

System Namespace