StackFrame クラス

現在のスレッドのコール スタック上での関数呼び出しを表す、 StackFrame に関する情報を提供します。

この型のすべてのメンバの一覧については、StackFrame メンバ を参照してください。

System.Object
   System.Diagnostics.StackFrame

<Serializable>
Public Class StackFrame
[C#]
[Serializable]
public class StackFrame
[C++]
[Serializable]
public __gc class StackFrame
[JScript]
public
   Serializable
class StackFrame

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

StackFrame は、スレッドの実行中に関数が呼び出されるたびに作成され、コール スタックにプッシュされます。スタック フレームには必ず MethodBase 情報が含まれ、オプションでファイル名、行番号、および列番号の情報も含まれます。 StackFrame 情報は、デバッグ ビルド構成と共に使用すると最も有益な情報となります。既定では、デバッグ ビルドにはデバッグ シンボルが含まれ、リリース ビルドには含まれません。デバッグ シンボルには、 StackFrame オブジェクトを構築するために使用されるファイル、メソッド名、行番号、および列情報のほとんどが含まれます。

使用例

[Visual Basic, C#, C++] StackFrame を作成して使用する方法を次のコード例で示します。

 
Imports System
Imports System.Diagnostics

Imports SampleInternal

Namespace SamplePublic
   
   ' This console application illustrates various uses
   ' of the StackTrace and StackFrame classes.
   
   Class ConsoleApp
      
      <STAThread()>  _
      Shared Sub Main()
         Dim mainClass As New ClassLevel1
         
         Try
            mainClass.InternalMethod()
         Catch
            Console.WriteLine(" Main method exception handler")
            
            ' Display file and line information, if available.
            Dim st As New StackTrace(New StackFrame(True))
            Console.WriteLine(" Stack trace for current level: {0}", _
               st.ToString())
            Console.WriteLine(" File: {0}", _
               st.GetFrame(0).GetFileName())
            Console.WriteLine(" Line Number: {0}", _
               st.GetFrame(0).GetFileLineNumber().ToString())
            
            Console.WriteLine()
            Console.WriteLine("-------------------------------------------------")
            Console.WriteLine()
         End Try
      End Sub 'Main
   End Class 'ConsoleApp
End Namespace 'StackFramePublic 

Namespace SampleInternal
   
   Public Class ClassLevel1
      
      Public Sub InternalMethod()
         Try
            Dim nestedClass As New ClassLevel2
            nestedClass.Level2Method()
         Catch e As Exception
            Console.WriteLine(" InternalMethod exception handler")
            
            ' Build a stack trace from one frame, skipping the 
            ' current frame and using the next frame.  By default,
            ' file and line information are not displayed.
            Dim st As New StackTrace(New StackFrame(1))
            Console.WriteLine(" Stack trace for next level frame: {0}", _
               st.ToString())
            Console.WriteLine(" Stack frame for next level: ")
            Console.WriteLine("   {0}", st.GetFrame(0).ToString())
            
            Console.WriteLine(" Line Number: {0}", _
               st.GetFrame(0).GetFileLineNumber().ToString())
            
            Console.WriteLine()
            Console.WriteLine("   ... throwing exception to next level ...")
            Console.WriteLine("-------------------------------------------------")
            Console.WriteLine()
            Throw e
         End Try
      End Sub 'InternalMethod
   End Class 'ClassLevel1
   
   Public Class ClassLevel2
      
      Public Sub Level2Method()
         Try
            Dim nestedClass As New ClassLevel3
            nestedClass.Level3Method()
         
         Catch e As Exception
            Console.WriteLine(" Level2Method exception handler")
            
            ' Display the full call stack at this level.
            Dim st1 As New StackTrace(True)
            Console.WriteLine(" Stack trace for this level: {0}", _
               st1.ToString())
            
            ' Build a stack trace from one frame, skipping the current
            ' frame and using the next frame.
            Dim st2 As New StackTrace(New StackFrame(1, True))
            Console.WriteLine(" Stack trace built with next level frame: {0}", _
                st2.ToString())
            
            ' Build a stack trace skipping the current frame, and
            ' including all the other frames.
            Dim st3 As New StackTrace(1, True)
            Console.WriteLine(" Stack trace built from the next level up: {0}", _
                st3.ToString())
            
            Console.WriteLine()
            Console.WriteLine("   ... throwing exception to next level ...")
            Console.WriteLine("-------------------------------------------------")
            Console.WriteLine()
            Throw e
         End Try
      End Sub 'Level2Method
   End Class 'ClassLevel2
   
   Public Class ClassLevel3
      
      Public Sub Level3Method()
         Try
            Dim nestedClass As New ClassLevel4()
            nestedClass.Level4Method()
         Catch e As Exception
            Console.WriteLine(" Level3Method exception handler")
            
            ' Build a stack trace from a dummy stack frame.
            ' Explicitly specify the source file name and line number.
            Dim st As New StackTrace(New StackFrame("source.cs", 60))
            Console.WriteLine(" Stack trace with dummy stack frame: {0}", _
               st.ToString())
            Dim i As Integer
            For i = 0 To st.FrameCount - 1
               ' Display the stack frame properties.
               Dim sf As StackFrame = st.GetFrame(i)
               Console.WriteLine(" File: {0}", sf.GetFileName())
               Console.WriteLine(" Line Number: {0}", _
                  sf.GetFileLineNumber())
               ' The column number defaults to zero when not initialized.
               Console.WriteLine(" Column Number: {0}", _
                  sf.GetFileColumnNumber())
               If sf.GetILOffset <> StackFrame.OFFSET_UNKNOWN
                  Console.WriteLine(" Intermediate Language Offset: {0}", _
                      sf.GetILOffset())
               End If
               If sf.GetNativeOffset <> StackFrame.OFFSET_UNKNOWN
                 Console.WriteLine(" Native Offset: {0}", _
                     sf.GetNativeOffset())
               End If
            Next i 
            Console.WriteLine()
            Console.WriteLine("   ... throwing exception to next level ...")
            Console.WriteLine("-------------------------------------------------")
            Console.WriteLine()
            Throw e
         End Try
      End Sub 'Level3Method
   End Class 'ClassLevel3
   
   Public Class ClassLevel4
      
      Public Sub Level4Method()
         Try
            Dim [nestedClass] As New ClassLevel5()
            [nestedClass].Level5Method()
         Catch e As Exception
            Console.WriteLine(" Level4Method exception handler")
            
            ' Build a stack trace from a dummy stack frame.
            ' Explicitly specify the source file name, line number
            ' and column number.
            Dim st As New StackTrace(New StackFrame("source.cs", 79, 24))
            Console.WriteLine(" Stack trace with dummy stack frame: {0}", _
               st.ToString())
            
            ' Access the StackFrames explicitly to display the file
            ' name, line number and column number properties.
            ' StackTrace.ToString only includes the method name. 
            Dim i As Integer
            For i = 0 To st.FrameCount - 1
               Dim sf As StackFrame = st.GetFrame(i)
               Console.WriteLine(" File: {0}", sf.GetFileName())
               Console.WriteLine(" Line Number: {0}", _
                  sf.GetFileLineNumber())
               Console.WriteLine(" Column Number: {0}", _
                  sf.GetFileColumnNumber())
            Next i
            Console.WriteLine()
            Console.WriteLine("   ... throwing exception to next level ...")
            Console.WriteLine("-------------------------------------------------")
            Console.WriteLine()
            Throw e
         End Try
      End Sub 'Level4Method 
   End Class 'ClassLevel4
  
   
   Public Class ClassLevel5
      
      Public Sub Level5Method()
         Try
            Dim nestedClass As New ClassLevel6()
            nestedClass.Level6Method()
         Catch e As Exception
            Console.WriteLine(" Level5Method exception handler")
            
            Dim st As New StackTrace()
            
            ' Display the most recent function call.
            Dim sf As StackFrame = st.GetFrame(0)
            Console.WriteLine()
            Console.WriteLine("  Exception in method: ")
            Console.WriteLine("      {0}", sf.GetMethod())
            
            If st.FrameCount > 1 Then
               ' Display the highest-level function call in the trace.
               sf = st.GetFrame((st.FrameCount - 1))
               Console.WriteLine("  Original function call at top of call stack):")
               Console.WriteLine("      {0}", sf.GetMethod())
            End If
            
            Console.WriteLine()
            Console.WriteLine("   ... throwing exception to next level ...")
            Console.WriteLine("-------------------------------------------------")
            Console.WriteLine()
            Throw e
         End Try
      End Sub 'Level5Method
   End Class 'ClassLevel5 
  
   
   Public Class ClassLevel6
      Public Sub Level6Method()
         Throw New Exception("An error occurred in the lowest internal class method.")
      End Sub 'Level6Method
   End Class 'ClassLevel6 

End Namespace 'StackFrameInternal

[C#] 

using System;
using System.Diagnostics;

using SampleInternal;

namespace SamplePublic
{
    // This console application illustrates various uses
    // of the StackTrace and StackFrame classes.
    class ConsoleApp
    {
       [STAThread]
       static void Main()
        {
            ClassLevel1 mainClass = new ClassLevel1();

            try {
                mainClass.InternalMethod();
            }
            catch (Exception) {
               Console.WriteLine(" Main method exception handler");

               // Display file and line information, if available.
               StackTrace st = new StackTrace(new StackFrame(true));
               Console.WriteLine(" Stack trace for current level: {0}",
                   st.ToString());
               Console.WriteLine(" File: {0}", 
                  st.GetFrame(0).GetFileName());
               Console.WriteLine(" Line Number: {0}",
                   st.GetFrame(0).GetFileLineNumber().ToString());

               Console.WriteLine();
               Console.WriteLine("-------------------------------------------------\n");
            }
        }
    }
}

namespace SampleInternal
{
   public class ClassLevel1
   {
      public void InternalMethod()
      {
         try
         {
            ClassLevel2 nestedClass = new ClassLevel2();
            nestedClass.Level2Method();
         }
         catch (Exception e)
         {
            Console.WriteLine(" InternalMethod exception handler");

            // Build a stack trace from one frame, skipping the
            // current frame and using the next frame.  By
            // default, file and line information are not displayed.
            StackTrace st = new StackTrace(new StackFrame(1));
            Console.WriteLine(" Stack trace for next level frame: {0}",
               st.ToString());
            Console.WriteLine(" Stack frame for next level: ");
            Console.WriteLine("   {0}", st.GetFrame(0).ToString());

            Console.WriteLine(" Line Number: {0}",
               st.GetFrame(0).GetFileLineNumber().ToString());

            Console.WriteLine();
            Console.WriteLine("   ... throwing exception to next level ...");
            Console.WriteLine("-------------------------------------------------\n");
            throw e;
         }
      }
   }

   public class ClassLevel2
   {
      public void Level2Method()
      {
         try 
         {
            ClassLevel3 nestedClass = new ClassLevel3();
            nestedClass.Level3Method();

         }
         catch (Exception e) 
         {
            Console.WriteLine(" Level2Method exception handler");

            // Display the full call stack at this level.
            StackTrace st1 = new StackTrace(true);
            Console.WriteLine(" Stack trace for this level: {0}",
               st1.ToString());

            // Build a stack trace from one frame, skipping the current
            // frame and using the next frame.
            StackTrace st2 = new StackTrace(new StackFrame(1, true));
            Console.WriteLine(" Stack trace built with next level frame: {0}",
               st2.ToString());

            // Build a stack trace skipping the current frame, and
            // including all the other frames.
            StackTrace st3 = new StackTrace(1, true);
            Console.WriteLine(" Stack trace built from the next level up: {0}",
               st3.ToString());

            Console.WriteLine();
            Console.WriteLine("   ... throwing exception to next level ...");
            Console.WriteLine("-------------------------------------------------\n");
            throw e;
         }
      }
   }

   public class ClassLevel3
   {
      public void Level3Method()
      {
         try 
         {
            ClassLevel4 nestedClass = new ClassLevel4();
            nestedClass.Level4Method();
         }
         catch (Exception e) 
         {
            Console.WriteLine(" Level3Method exception handler");

            // Build a stack trace from a dummy stack frame.
            // Explicitly specify the source file name and 
            // line number.
            StackTrace st = new StackTrace(new StackFrame("source.cs", 60));
            Console.WriteLine(" Stack trace with dummy stack frame: {0}", 
                        st.ToString());
            for(int i =0; i< st.FrameCount; i++ )
            {
               // Display the stack frame properties.
               StackFrame sf = st.GetFrame(i);
               Console.WriteLine(" File: {0}", sf.GetFileName());
               Console.WriteLine(" Line Number: {0}", 
                  sf.GetFileLineNumber());
               // Note that the column number defaults to zero
               // when not initialized.
               Console.WriteLine(" Column Number: {0}", 
                  sf.GetFileColumnNumber());
               if (sf.GetILOffset() != StackFrame.OFFSET_UNKNOWN)
               {
                  Console.WriteLine(" Intermediate Language Offset: {0}", 
                     sf.GetILOffset());
               }
               if (sf.GetNativeOffset() != StackFrame.OFFSET_UNKNOWN)
               {
                  Console.WriteLine(" Native Offset: {0}", 
                     sf.GetNativeOffset());
               }
            }
            Console.WriteLine();
            Console.WriteLine("   ... throwing exception to next level ...");
            Console.WriteLine("-------------------------------------------------\n");
            throw e;
         }
      }
   }

   public class ClassLevel4
   {
      public void Level4Method()
      {
         try 
         {
            ClassLevel5 nestedClass = new ClassLevel5();
            nestedClass.Level5Method();
         }
         catch (Exception e) 
         {
            Console.WriteLine(" Level4Method exception handler");

            // Build a stack trace from a dummy stack frame.
            // Explicitly specify the source file name, line number
            // and column number.
            StackTrace st = new StackTrace(new StackFrame("source.cs", 79, 24));
            Console.WriteLine(" Stack trace with dummy stack frame: {0}", 
                           st.ToString());

            // Access the StackFrames explicitly to display the file
            // name, line number and column number properties.
            // StackTrace.ToString only includes the method name. 
            for(int i =0; i< st.FrameCount; i++ )
            {
               StackFrame sf = st.GetFrame(i);
               Console.WriteLine(" File: {0}", sf.GetFileName());
               Console.WriteLine(" Line Number: {0}", 
                  sf.GetFileLineNumber());
               Console.WriteLine(" Column Number: {0}", 
                  sf.GetFileColumnNumber());
            }
            Console.WriteLine();
            Console.WriteLine("   ... throwing exception to next level ...");
            Console.WriteLine("-------------------------------------------------\n");
            throw e;
         }
      }
   }

   public class ClassLevel5
   {
      public void Level5Method()
      {
         try 
         {
            ClassLevel6 nestedClass = new ClassLevel6();
            nestedClass.Level6Method();
         }
         catch (Exception e) 
         {
            Console.WriteLine(" Level5Method exception handler");

            StackTrace st = new StackTrace();
            
            // Display the most recent function call.
            StackFrame sf = st.GetFrame(0);
            Console.WriteLine();
            Console.WriteLine("  Exception in method: ");
            Console.WriteLine("      {0}", sf.GetMethod());

            if (st.FrameCount >1)
            {
               // Display the highest-level function call 
               // in the trace.
               sf = st.GetFrame(st.FrameCount-1);
               Console.WriteLine("  Original function call at top of call stack):");
               Console.WriteLine("      {0}", sf.GetMethod());
            }

            Console.WriteLine();
            Console.WriteLine("   ... throwing exception to next level ...");
            Console.WriteLine("-------------------------------------------------\n");
            throw e;
         }        
      }

   }

   public class ClassLevel6
   {
      public void Level6Method()
      {
         throw new Exception("An error occurred in the lowest internal class method.");
      }

   }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Diagnostics;

// This console application illustrates various uses
// of the StackTrace and StackFrame classes.

namespace SampleInternal
{
   public __gc class ClassLevel6
   {
      public:
      void Level6Method()
      {
         throw new Exception(S"An error occurred in the lowest internal class method.");
      }

   };

   public __gc class ClassLevel5
   {
      public:
      void Level5Method()
      {
         try 
         {
            ClassLevel6 *nestedClass = new ClassLevel6();
            nestedClass->Level6Method();
         }
         catch (Exception *e) 
         {
            Console::WriteLine(S" Level5Method exception handler");

            StackTrace *st = new StackTrace();
            
            // Display the most recent function call.
            StackFrame *sf = st->GetFrame(0);
            Console::WriteLine();
            Console::WriteLine(S"  Exception in method: ");
            Console::WriteLine(S"      {0}", sf->GetMethod());

            if (st->FrameCount >1)
            {
               // Display the highest-level function call
               // in the trace.
               sf = st->GetFrame(st->FrameCount-1);
               Console::WriteLine(S"  Original function call at top of call stack):");
               Console::WriteLine(S"      {0}", sf->GetMethod());
            }

            Console::WriteLine();
            Console::WriteLine(S"   ... throwing exception to next level ...");
            Console::WriteLine(S"-------------------------------------------------\n");
            throw e;
         }        
      }
   };

   public __gc class ClassLevel4
   {
      public:

      void Level4Method()
      {
         try 
         {
            ClassLevel5 *nestedClass = new ClassLevel5();
            nestedClass->Level5Method();
         }
         catch (Exception *e) 
         {
            Console::WriteLine(S" Level4Method exception handler");

            // Build a stack trace from a dummy stack frame.
            // Explicitly specify the source file name, line number
            // and column number.
            StackTrace *st = new StackTrace(new StackFrame(S"source.cs", 79, 24));
            Console::WriteLine(S" Stack trace with dummy stack frame: {0}", 
                           st->ToString());

            // Access the StackFrames explicitly to display the file
            // name, line number and column number properties.
            // StackTrace.ToString only includes the method name. 
            for(int i =0; i< st->FrameCount; i++ )
            {
               StackFrame *sf = st->GetFrame(i);
               Console::WriteLine(S" File: {0}", sf->GetFileName());
               Console::WriteLine(S" Line Number: {0}", 
                  sf->GetFileLineNumber().ToString());
               Console::WriteLine(S" Column Number: {0}", 
                  sf->GetFileColumnNumber().ToString());
            }
            Console::WriteLine();
            Console::WriteLine(S"   ... throwing exception to next level ...");
            Console::WriteLine(S"-------------------------------------------------\n");
            throw e;
         }
      }
   };


   public __gc class ClassLevel3
   {
      public:
      void Level3Method()
      {
         try 
         {
            ClassLevel4 *nestedClass = new ClassLevel4();
            nestedClass->Level4Method();
         }
         catch (Exception *e) 
         {
            Console::WriteLine(S" Level3Method exception handler");

            // Build a stack trace from a dummy stack frame.
            // Explicitly specify the source file name and line number.
            StackTrace *st = new StackTrace(new StackFrame(S"source.cs", 60));
            Console::WriteLine(S" Stack trace with dummy stack frame: {0}", 
                        st->ToString());
            for(int i =0; i< st->FrameCount; i++ )
            {
               // Display the stack frame properties.
               StackFrame *sf = st->GetFrame(i);
               Console::WriteLine(S" File: {0}", sf->GetFileName());
               Console::WriteLine(S" Line Number: {0}", 
                  sf->GetFileLineNumber().ToString());
               // Note that the column number defaults to zero
               // when not initialized.
               Console::WriteLine(S" Column Number: {0}", 
                  sf->GetFileColumnNumber().ToString());
               Console::WriteLine(S" Intermediate Language Offset: {0}", 
                  sf->GetILOffset().ToString());
               Console::WriteLine(S" Native Offset: {0}", 
                  sf->GetNativeOffset().ToString());
            }
            Console::WriteLine();
            Console::WriteLine(S"   ... throwing exception to next level ...");
            Console::WriteLine(S"-------------------------------------------------\n");
            throw e;
         }
      }
   };

   public __gc class ClassLevel2
   {
      public:
      void Level2Method()
      {
         try 
         {
            ClassLevel3 *nestedClass = new ClassLevel3();
            nestedClass->Level3Method();

         }
         catch (Exception *e) 
         {
            Console::WriteLine(S" Level2Method exception handler");

            // Display the full call stack at this level.
            StackTrace *st1 = new StackTrace(true);
            Console::WriteLine(S" Stack trace for this level: {0}", 
               st1->ToString());

            // Build a stack trace from one frame, skipping the
            // current frame and using the next frame.
            StackTrace *st2 = new StackTrace(new StackFrame(1, true));
            Console::WriteLine(S" Stack trace built with next level frame: {0}", 
               st2->ToString());

            // Build a stack trace skipping the current frame, and
            // including all the other frames.
            StackTrace *st3 = new StackTrace(1, true);
            Console::WriteLine(S" Stack trace built from the next level up: {0}", 
               st3->ToString());

            Console::WriteLine();
            Console::WriteLine(S"   ... throwing exception to next level ...");
            Console::WriteLine(S"-------------------------------------------------\n");
            throw e;
         }
      }
   };

   public __gc class ClassLevel1
   {
      public:
      void InternalMethod()
      {
         try
         {
            ClassLevel2 *nestedClass = new ClassLevel2();
            nestedClass->Level2Method();
         }
         catch (Exception *e)
         {
            Console::WriteLine(S" InternalMethod exception handler");

            // Build a stack trace from one frame, skipping the
            // current frame and using the next frame.  By
            // default, file and line information are not displayed.
            StackTrace *st = new StackTrace(new StackFrame(1));
            Console::WriteLine(S" Stack trace for next level frame: {0}", 
               st->ToString());
            Console::WriteLine(S" Stack frame for next level: ");
            Console::WriteLine(S"   {0}", st->GetFrame(0)->ToString());

            Console::WriteLine(S" Line Number: {0}", 
               st->GetFrame(0)->GetFileLineNumber().ToString());

            Console::WriteLine();
            Console::WriteLine(S"   ... throwing exception to next level ...");
            Console::WriteLine(S"-------------------------------------------------\n");
            throw e;
         }
      }
   };
}

using namespace SampleInternal;
namespace SamplePublic
{
    class ConsoleApp
    {
    public:
       [STAThread]
       static void Main()
        {
            ClassLevel1 *mainClass = new ClassLevel1();

            try {
                mainClass->InternalMethod();
            }
            catch (Exception *e) {
               Console::WriteLine(S" Main method exception handler");

               // Display file and line information, if available.
                StackTrace *st = new StackTrace(new StackFrame(true));
                Console::WriteLine(S" Stack trace for current level: {0}",
                   st->ToString());
               Console::WriteLine(S" File: {0}", 
                  st->GetFrame(0)->GetFileName());
               Console::WriteLine(S" Line Number: {0}",
                   st->GetFrame(0)->GetFileLineNumber().ToString());

               Console::WriteLine();
               Console::WriteLine(S"-------------------------------------------------\n");
            }
        }
    };
}

int main() {

   SamplePublic::ConsoleApp::Main();
};

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Diagnostics

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

StackFrame メンバ | System.Diagnostics 名前空間 | StackTrace