Gets the Exception instance that caused the current Exception.
An instance of Exception that describes the error that caused the current Exception.
This property is read-only.
The Exception.InnerException property returns the same value as was passed into the constructor, or null if the inner exception value was not supplied to the constructor.
The following example demonstrates throwing and catching an Exception that references an inner Exception.
C# Example
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);
}
}
}
The output is
Example
In Main catch block. Caught: Error caused by trying ThrowInner. Inner Exception is MyAppException: ExceptExample inner exception at ExceptExample.ThrowInner() at ExceptExample.CatchInner()