本文整理汇总了C#中System.Exception.Data属性的典型用法代码示例。如果您正苦于以下问题:C# Exception.Data属性的具体用法?C# Exception.Data怎么用?C# Exception.Data使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Exception
的用法示例。
在下文中一共展示了Exception.Data属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
// This example demonstrates the Exception.Data property.
using System;
using System.Collections;
class Sample
{
public static void Main()
{
Console.WriteLine("\nException with some extra information...");
RunTest(false);
Console.WriteLine("\nException with all extra information...");
RunTest(true);
}
public static void RunTest(bool displayDetails)
{
try {
NestedRoutine1(displayDetails);
}
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);
}
}
}
public static void NestedRoutine1(bool displayDetails)
{
try {
NestedRoutine2(displayDetails);
}
catch (Exception e) {
e.Data["ExtraInfo"] = "Information from NestedRoutine1.";
e.Data.Add("MoreExtraInfo", "More information from NestedRoutine1.");
throw;
}
}
public static void NestedRoutine2(bool displayDetails)
{
Exception e = new Exception("This statement is the original exception message.");
if (displayDetails) {
string s = "Information from NestedRoutine2.";
int i = -903;
DateTime dt = DateTime.Now;
e.Data.Add("stringInfo", s);
e.Data["IntInfo"] = i;
e.Data["DateTimeInfo"] = dt;
}
throw e;
}
}
输出:
Exception with some extra information... An exception was thrown. This statement is the original exception message. Extra details: Key: 'ExtraInfo' Value: Information from NestedRoutine1. Key: 'MoreExtraInfo' Value: More information from NestedRoutine1. Exception with all extra information... An exception was thrown. This statement is the original exception message. Extra details: Key: 'stringInfo' Value: Information from NestedRoutine2. Key: 'IntInfo' Value: -903 Key: 'DateTimeInfo' Value: 7/29/2013 10:50:13 AM Key: 'ExtraInfo' Value: Information from NestedRoutine1. Key: 'MoreExtraInfo' Value: More information from NestedRoutine1.