C# Exception Data

Description

Exception Data gets a collection of key/value pairs that provide additional user-defined information about the exception.

Syntax

Exception.Data has the following syntax.


public virtual IDictionary Data { get; }

Example

The following example demonstrates how to add and retrieve information using the Data property.


using System;/*from   www.  ja  va 2s.  co m*/
using System.Collections;

class Sample 
{
   public static void Main()
   {
      try {
          Exception e = new Exception("This statement is the original exception message.");
          string s = "Information.";
          int i = -903;
          DateTime dt = DateTime.Now;
          e.Data.Add("stringInfo", s);
          e.Data["IntInfo"] = i;
          e.Data["DateTimeInfo"] = dt;
          e.HelpLink = "http://java2s.com";
          throw e;
      }
      catch (Exception e) {
         Console.WriteLine("An exception was thrown.");
         Console.WriteLine(e.Message);
         if (e.Data.Count > 0) {
            Console.WriteLine("  Extra details:");
            foreach (DictionaryEntry de in e.Data)
               Console.WriteLine("    Key: {0,-20}      Value: {1}", 
                                 "'" + de.Key.ToString() + "'", de.Value);
         }
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version