Mono Class Library: System.Exception Overview | Members

System.Exception.InnerException Property

Gets the Exception instance that caused the current Exception.

public Exception InnerException { get; }

Value

An instance of Exception that describes the error that caused the current Exception.

Remarks

This property is read-only.

Note: When an Exception X is thrown as a direct result of a previous exception Y , the Exception.InnerException property of X should contain a reference to Y .

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.

Note: Using the Exception.InnerException property, you can obtain the set of Exceptions that led to the current Exception. Exception.GetBaseException includes an example that demonstrates this procedure.

Example

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()
 

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0