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.
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

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

   

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



Exception.Data プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS