Data Property
It is possible to store structured data on an exception that is thrown in one part of your program, and then later read in this data. With Data we store associate keys and values. In the following example, a new exception is allocated and then some value is assigned to the Data property.
using System;
using System.Collections;
class Program
{
static void Main()
{
try
{
// Create new exception.
var ex = new DivideByZeroException("Message");
// Set the data dictionary.
ex.Data["Time"] = DateTime.Now;
ex.Data["Flag"] = true;
// Throw it.
throw ex;
}
catch (Exception ex)
{
// Display the exception's data dictionary.
foreach (DictionaryEntry pair in ex.Data)
{
Console.WriteLine("{0} = {1}", pair.Key, pair.Value);
}
}
}
}
Output
Time = 12/9/2018 10:10:10 AM
Flag = True