Exception.Data 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得索引鍵/值組的集合,提供例外狀況的其他使用者定義資訊。
public:
virtual property System::Collections::IDictionary ^ Data { System::Collections::IDictionary ^ get(); };
public virtual System.Collections.IDictionary Data { get; }
member this.Data : System.Collections.IDictionary
Public Overridable ReadOnly Property Data As IDictionary
屬性值
一個實作介面 IDictionary 的物件,包含一組使用者定義的鍵值對。 預設是空集合。
範例
以下範例示範如何使用該 Data 屬性新增與檢索資訊。
// 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;
}
}
// The example displays the following output:
// 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.
// This example demonstrates the Exception.Data property.
open System
open System.Collections
let nestedRoutine2 displayDetails =
let e = Exception "This statement is the original exception message."
if displayDetails then
let s = "Information from nestedRoutine2."
let i = -903
let dt = DateTime.Now
e.Data.Add("stringInfo", s)
e.Data["IntInfo"] <- i
e.Data["DateTimeInfo"] <- dt
raise e
let nestedRoutine1 displayDetails =
try
nestedRoutine2 displayDetails
with e ->
e.Data["ExtraInfo"] <- "Information from nestedRoutine1."
e.Data.Add("MoreExtraInfo", "More information from nestedRoutine1.")
reraise ()
let runTest displayDetails =
try
nestedRoutine1 displayDetails
with e ->
printfn "An exception was thrown."
printfn $"{e.Message}"
if e.Data.Count > 0 then
printfn " Extra details:"
for de in e.Data do
let de = de :?> DictionaryEntry
printfn $""" Key: {"'" + de.Key.ToString() + "'",-20} Value: {de.Value}"""
printfn "\nException with some extra information..."
runTest false
printfn "\nException with all extra information..."
runTest true
// The example displays the following output:
// 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.
Imports System.Collections
Module Example
Public Sub Main()
Console.WriteLine()
Console.WriteLine("Exception with some extra information...")
RunTest(False)
Console.WriteLine()
Console.WriteLine("Exception with all extra information...")
RunTest(True)
End Sub
Public Sub RunTest(displayDetails As Boolean)
Try
NestedRoutine1(displayDetails)
Catch e As Exception
Console.WriteLine("An exception was thrown.")
Console.WriteLine(e.Message)
If e.Data.Count > 0 Then
Console.WriteLine(" Extra details:")
For Each de As DictionaryEntry In e.Data
Console.WriteLine(" Key: {0,-20} Value: {1}",
"'" + de.Key.ToString() + "'", de.Value)
Next
End If
End Try
End Sub
Public Sub NestedRoutine1(displayDetails As Boolean)
Try
NestedRoutine2(displayDetails)
Catch e As Exception
e.Data("ExtraInfo") = "Information from NestedRoutine1."
e.Data.Add("MoreExtraInfo", "More information from NestedRoutine1.")
Throw e
End Try
End Sub
Public Sub NestedRoutine2(displayDetails As Boolean)
Dim e As New Exception("This statement is the original exception message.")
If displayDetails Then
Dim s As String = "Information from NestedRoutine2."
Dim i As Integer = -903
Dim dt As DateTime = DateTime.Now
e.Data.Add("stringInfo", s)
e.Data("IntInfo") = i
e.Data("DateTimeInfo") = dt
End If
Throw e
End Sub
End Module
' This example displays the following output:
' 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.
備註
欲了解更多關於此 API 的資訊,請參閱 Exception.Data 的補充 API 備註。