Exception.Data プロパティ
アセンブリ: mscorlib (mscorlib.dll 内)


Data プロパティによって返される System.Collections.IDictionary オブジェクトを使用し、例外に関する補足情報を格納および取得します。情報の形式は、任意の数のユーザー定義のキー/値ペアです。ペアの値コンポーネントが任意の型のオブジェクトであるのに対して、各キー/値ペアのキー コンポーネントは、通常、識別文字列です。
キー/値ペアのセキュリティData プロパティによって返されるコレクション内に格納されているキー/値ペアは安全ではありません。入れ子になった一連のルーチンをアプリケーションで呼び出したときに、各ルーチンに例外ハンドラが格納されている場合は、結果のコール スタックにこれらの例外ハンドラの階層が格納されます。下位レベルのルーチンが例外をスローした場合、コース スタック階層内の上位レベルの例外ハンドラは、他の例外ハンドラがコレクションに格納したキー/値ペアの読み取りや変更を行うことができます。そのため、キー/値ペア内の情報が機密ではないことを明確にして、キー/値ペア内の情報が壊れた場合にアプリケーションが正常に動作することを保障する必要があります。
キーの競合は、異なる例外ハンドラがキー/値ペアへのアクセス用に同じキーを指定した場合に発生します。キーの競合の結果として、下位レベルの例外ハンドラが誤って上位レベルの例外ハンドラとやり取りする可能性があります。このやりとりが軽度のプログラム エラーを招く場合があるので、アプリケーションの開発時は注意が必要です。ただし、キーの競合は、十分に注意しながら扱えばアプリケーションの拡張に利用できます。
キーの競合を回避するには、キー/値ペアの一意のキーを生成する名前付け規則を適用します。たとえば、名前付け規則によって、アプリケーションのピリオド区切りの名前、ペアの補足情報を提供するメソッド、および一意の識別子で構成されるキーが生成されます。
ここで、Products および Suppliers という名前の 2 つのアプリケーションがそれぞれ Sales という名前のメソッドを持っている場合を考えてみましょう。Products アプリケーション内のSales メソッドは、製品の識別番号 (在庫管理単位つまり SKU) を提供しますSuppliers アプリケーションの Sales メソッドは、仕入先の識別番号、つまり SID を提供します。この例に名前付け規則を適用すると、"Products.Sales.SKU" および "Suppliers.Sales.SID" というキーが生成されます。
処理を制御する特別な事前配置キーが 1 つ以上存在することを利用して、キーの競合を活用できます。たとえば、1 つのシナリオとして、コール スタック 階層内の最上位レベルの例外ハンドラが、下位レベルの例外ハンドラによってスローされた例外をすべてキャッチするとします。特別なキーを持つキー/値ペアが存在する場合、上位レベルの例外ハンドラは、IDictionary オブジェクト内の残りのキー/値ペアの形式を通常とは異なる手法で設定します。それ以外の場合、残りのキー/値ペアは通常の手法で形式が設定されます。
ここで、別のシナリオについて考えてみましょう。コール スタック階層の各レベルの例外ハンドラが、次に低いレベルの例外ハンドラによってスローされた例外をキャッチするとします。各例外ハンドラは、Data プロパティによって返されるコレクションに格納されたキー/値ペアのセットへのアクセスに事前配置キーのセットを使用できることを知っています。
各例外ハンドラは、事前配置キーのセットを使用して、対応するキー/値ペアの値コンポーネントをその例外ハンドラに固有の情報で更新します。更新プロセスが完了すると、例外ハンドラは、次にレベルの高い例外ハンドラに例外をスローします。最後に、最上位レベルの例外ハンドラは、キー/値ペアにアクセスし、すべての下位レベルの例外ハンドラからの更新情報を統合して表示します。
![]() |
---|
ExecutionEngineException、OutOfMemoryException、StackOverflowException、および ThreadAbortException の各クラスは、Data プロパティの値に対して常に null 参照 (Visual Basic では Nothing) を返します。 |

Data プロパティを使用して情報を追加および取得する方法を次の例に示します。
' This example demonstrates the Exception.Data property. Imports System Imports System.Collections Class Sample Public Shared 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 'Main Public Shared Sub RunTest(displayDetails As Boolean) Try NestedRoutine1(displayDetails) Catch e As Exception Console.WriteLine("An exception was thrown.") Console.WriteLine(e.Message) If Not (e.Data Is Nothing) Then Console.WriteLine(" Extra details:") Dim de As DictionaryEntry For Each de In e.Data Console.WriteLine(" The key is '{0}' and the value is: {1}", de.Key, de.Value) Next de End If End Try End Sub 'RunTest Public Shared 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 'NestedRoutine1 Public Shared 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 'NestedRoutine2 End Class 'Sample ' 'This example produces the following results: ' 'Exception with some extra information... 'An exception was thrown. 'This statement is the original exception message. ' Extra details: ' The key is 'ExtraInfo' and the value is: Information from NestedRoutine1. ' The key is 'MoreExtraInfo' and the value is: More information from NestedRoutine1. ' 'Exception with all extra information... 'An exception was thrown. 'This statement is the original exception message. ' Extra details: ' The key is 'stringInfo' and the value is: Information from NestedRoutine2. ' The key is 'IntInfo' and the value is: -903 ' The key is 'DateTimeInfo' and the value is: 11/26/2002 2:12:58 PM ' The key is 'ExtraInfo' and the value is: Information from NestedRoutine1. ' The key is 'MoreExtraInfo' and the value is: More information from NestedRoutine1. '
// This example demonstrates the Exception.Data property. using System; using System.Collections; class Sample { public static void Main() { Console.WriteLine(); Console.WriteLine("Exception with some extra information..."); RunTest(false); Console.WriteLine(); Console.WriteLine("Exception 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 != null) { Console.WriteLine(" Extra details:"); foreach (DictionaryEntry de in e.Data) Console.WriteLine(" The key is '{0}' and the value is: {1}", de.Key, 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 e; } } 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; } } /* This example produces the following results: Exception with some extra information... An exception was thrown. This statement is the original exception message. Extra details: The key is 'ExtraInfo' and the value is: Information from NestedRoutine1. The key is 'MoreExtraInfo' and the value is: More information from NestedRoutine1. Exception with all extra information... An exception was thrown. This statement is the original exception message. Extra details: The key is 'stringInfo' and the value is: Information from NestedRoutine2. The key is 'IntInfo' and the value is: -903 The key is 'DateTimeInfo' and the value is: 11/26/2002 2:12:58 PM The key is 'ExtraInfo' and the value is: Information from NestedRoutine1. The key is 'MoreExtraInfo' and the value is: More information from NestedRoutine1. */
// This example demonstrates the Exception.Data property. using namespace System; using namespace System::Collections; void NestedRunTest( bool displayDetails ); // forward declarations void NestedRoutine1( bool displayDetails ); void NestedRoutine2( bool displayDetails ); void RunTest( bool displayDetails ); int main() { Console::WriteLine(); Console::WriteLine( "Exception with some extra information..." ); RunTest( false ); Console::WriteLine(); Console::WriteLine( "Exception with all extra information..." ); RunTest( true ); } void RunTest( bool displayDetails ) { try { NestedRoutine1( displayDetails ); } catch ( Exception^ e ) { Console::WriteLine( "An exception was thrown." ); Console::WriteLine( e->Message ); if ( e->Data != nullptr ) { Console::WriteLine( " Extra details:" ); for each (DictionaryEntry de in e->Data) Console::WriteLine(" The key is '{0}' and the value is: {1}", de.Key, de.Value); } } } 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 e; } } void NestedRoutine2( bool displayDetails ) { Exception^ e = gcnew 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; } /* This example produces the following results: Exception with some extra information... An exception was thrown. This statement is the original exception message. Extra details: The key is 'ExtraInfo' and the value is: Information from NestedRoutine1. The key is 'MoreExtraInfo' and the value is: More information from NestedRoutine1. Exception with all extra information... An exception was thrown. This statement is the original exception message. Extra details: The key is 'stringInfo' and the value is: Information from NestedRoutine2. The key is 'IntInfo' and the value is: -903 The key is 'DateTimeInfo' and the value is: 11/26/2002 2:12:58 PM The key is 'ExtraInfo' and the value is: Information from NestedRoutine1. The key is 'MoreExtraInfo' and the value is: More information from NestedRoutine1. */
// This example demonstrates the Exception.Data property. import System.*; import System.Collections.*; class Sample { public static void main(String[] args) { Console.WriteLine(); Console.WriteLine("Exception with some extra information..."); RunTest(false); Console.WriteLine(); Console.WriteLine("Exception with all extra information..."); RunTest(true); } //main public static void RunTest(boolean displayDetails) { try { NestedRoutine1(displayDetails); } catch (System.Exception e) { Console.WriteLine("An exception was thrown."); Console.WriteLine(e.get_Message()); if (e.get_Data() != null) { Console.WriteLine(" Extra details:"); DictionaryEntry de; IEnumerator enumObj = e.get_Data().GetEnumerator(); while (enumObj.MoveNext()) { de = (DictionaryEntry)enumObj.get_Current(); Console.WriteLine(" The key is '{0}' and the value " + "is: {1}", de.get_Key(), de.get_Value()); } } } } //RunTest public static void NestedRoutine1(boolean displayDetails) throws System.Exception { try { NestedRoutine2(displayDetails); } catch (System.Exception e) { e.get_Data().set_Item("ExtraInfo", "Information from NestedRoutine1."); e.get_Data().Add("MoreExtraInfo", "More information from NestedRoutine1."); throw e; } } //NestedRoutine1 public static void NestedRoutine2(boolean displayDetails) throws System.Exception { System.Exception e = new Exception("This statement is the original " + "exception message."); if (displayDetails) { String s = "Information from NestedRoutine2."; int i = -903; DateTime dt = DateTime.get_Now(); e.get_Data().Add("stringInfo", s); e.get_Data().set_Item("IntInfo", (Int32)i); e.get_Data().set_Item("DateTimeInfo", dt); } throw e; } //NestedRoutine2 } //Sample /* This example produces the following results: Exception with some extra information... An exception was thrown. This statement is the original exception message. Extra details: The key is 'ExtraInfo' and the value is: Information from NestedRoutine1. The key is 'MoreExtraInfo' and the value is: More information from NestedRoutine1. Exception with all extra information... An exception was thrown. This statement is the original exception message. Extra details: The key is 'stringInfo' and the value is: Information from NestedRoutine2. The key is 'IntInfo' and the value is: -903 The key is 'DateTimeInfo' and the value is: 11/26/2002 2:12:58 PM The key is 'ExtraInfo' and the value is: Information from NestedRoutine1. The key is 'MoreExtraInfo' and the value is: More information from NestedRoutine1. */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Exception.Data プロパティのページへのリンク