Exception.Data プロパティとは? わかりやすく解説

Exception.Data プロパティ

メモ : このプロパティは、.NET Framework version 2.0 で新しく追加されたものです。

例外に関する追加のユーザー定義情報を提供するキー/値ペアのコレクションを取得します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Overridable ReadOnly
 Property Data As IDictionary
Dim instance As Exception
Dim value As IDictionary

value = instance.Data
public virtual IDictionary Data { get; }
public:
virtual property IDictionary^ Data {
    IDictionary^ get ();
}
/** @property */
public IDictionary get_Data ()

プロパティ値
System.Collections.IDictionary インターフェイスを実装し、ユーザー定義のキー/値ペアのコレクションを格納するオブジェクト。既定値は空のコレクションです。

解説解説

Data プロパティによって返される System.Collections.IDictionary オブジェクトを使用し、例外に関する補足情報を格納および取得します。情報の形式は、任意の数のユーザー定義のキー/値ペアです。ペアの値コンポーネントが任意の型のオブジェクトであるのに対して、各キー/値ペアのキー コンポーネントは、通常、識別文字列です。

キー/値ペアのセキュリティ
キーの競合
キーの競合の回避
キーの競合の利用

処理を制御する特別な事前配置キーが 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.
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

Exception.Data プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Exception.Data プロパティのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS