Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Exception.InnerException Property

Gets the Exception instance that caused the current exception.

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

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.



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.

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

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() 
         catch (Exception e) 
         throw new MyAppException("Error caused by trying ThrowInner.",e);
public class Test 
   public static void Main() 
   ExceptExample testInstance = new ExceptExample();
         catch(Exception e) 
         Console.WriteLine ("In Main catch block. Caught: {0}", e.Message);
         Console.WriteLine ("Inner Exception is {0}",e.InnerException);

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
© 2018 Microsoft