StackTrace クラスとは? わかりやすく解説

StackTrace クラス

スタック トレース表しますスタック トレースは、順番並べられ1 つまたは複数スタック フレームコレクションです。

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

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class StackTrace
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class StackTrace
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class StackTrace
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class StackTrace
SerializableAttribute 
ComVisibleAttribute(true) 
public class StackTrace
解説解説
使用例使用例

単純な StackTrace作成し、そのフレーム反復処理してデバッグ情報診断情報取得する方法次のコンソール アプリケーション示します

Imports System
Imports System.Diagnostics

Class StackTraceSample
   
    <STAThread()>  _
    Public Shared Sub Main()

        Dim sample As New
 StackTraceSample()
        Try
   
            sample.MyPublicMethod()
        Catch
            ' Create a StackTrace that captures
            ' filename, line number, and column
            ' information for the current thread.
            Dim st As New
 StackTrace(True)
            Dim i As Integer

            For i = 0 To st.FrameCount - 1

                ' Note that high up the call stack, there is only
                ' one stack frame.
                Dim sf As StackFrame = st.GetFrame(i)
                Console.WriteLine()
                Console.WriteLine("High up the call stack, Method:
 {0}", _
                    sf.GetMethod())
            
                Console.WriteLine("High up the call stack, Line
 Number: {0}", _
                    sf.GetFileLineNumber())
            Next i
        End Try
    End Sub
   
   Public Sub MyPublicMethod()
      MyProtectedMethod()
   End Sub
    
   Protected Sub MyProtectedMethod()
      Dim mic As New MyInternalClass()
      mic.ThrowsException()
   End Sub
   
   
   Class MyInternalClass
      
      Public Sub ThrowsException()
         Try
            Throw New Exception("A
 problem was encountered.")
         Catch e As Exception

            ' Create a StackTrace that captures filename,
            ' line number and column information.
            Dim st As New
 StackTrace(True)

            Dim stackIndent As String
 = ""
            Dim i As Integer
            For i = 0 To st.FrameCount - 1
               ' Note that at this level, there are four
               ' stack frames, one for each method invocation.
               Dim sf As StackFrame = st.GetFrame(i)
               Console.WriteLine()
               Console.WriteLine(stackIndent + " Method: {0}",
 _
                   sf.GetMethod())
               Console.WriteLine(stackIndent + " File: {0}",
 _
                   sf.GetFileName())
               Console.WriteLine(stackIndent + " Line Number:
 {0}", _
                   sf.GetFileLineNumber())
               stackIndent += "  "
            Next i
            Throw e
         End Try
      End Sub
   End Class
End Class


' This console application produces the following output when
' compiled with the Debug configuration.
'   
'    Method: Void ThrowsException()
'    File: c:\pp\samples\stacktraceframe\myclass.vb
'    Line Number: 55
' 
'      Method: Void MyProtectedMethod()
'      File: c:\pp\samples\stacktraceframe\myclass.vb
'      Line Number: 42
' 
'        Method: Void MyPublicMethod()
'        File: c:\pp\samples\stacktraceframe\myclass.vb
'        Line Number: 37
' 
'          Method: Void Main(System.String[])
'          File: c:\pp\samples\stacktraceframe\myclass.vb
'          Line Number: 13
' 
'   High up the call stack, Method: Void Main(System.String[])
'   High up the call stack, Line Number: 18
' 
' 
' This console application produces the following output when
' compiled with the Release configuration.
' 
'    Method: Void ThrowsException()
'    File:
'    Line Number: 0
' 
'      Method: Void Main(System.String[])
'      File:
'      Line Number: 0
' 
'   High up the call stack, Method: Void Main()
'   High up the call stack, Line Number: 0
'
using System;
using System.Diagnostics;

class StackTraceSample
{
    [STAThread]
    static void Main(string[]
 args)
    {
        StackTraceSample sample = new StackTraceSample();
        try
        {
            sample.MyPublicMethod();
        }
        catch (Exception)
        {
            // Create a StackTrace that captures
            // filename, line number, and column
            // information for the current thread.
            StackTrace st = new StackTrace(true);
            for(int i =0; i< st.FrameCount;
 i++ )
            {
                // Note that high up the call stack, there is only
                // one stack frame.
                StackFrame sf = st.GetFrame(i);
                Console.WriteLine();
                Console.WriteLine("High up the call stack, Method: {0}"
,
                    sf.GetMethod());

                Console.WriteLine("High up the call stack, Line Number: {0}"
,
                    sf.GetFileLineNumber());
            }
        }
    }

    public void MyPublicMethod () 
    { 
        MyProtectedMethod(); 
    }

    protected void MyProtectedMethod ()
    {
        MyInternalClass mic = new MyInternalClass();
        mic.ThrowsException();
    }

    class MyInternalClass
    {
        public void ThrowsException()
        {
            try
            {
                throw new Exception("A problem was encountered.");
            }
            catch (Exception e)
            {
                // Create a StackTrace that captures filename,
                // line number and column information.
                StackTrace st = new StackTrace(true);
                string stackIndent = "";
                for(int i =0; i< st.FrameCount;
 i++ )
                {
                    // Note that at this level, there are four
                    // stack frames, one for each method invocation.
                    StackFrame sf = st.GetFrame(i);
                    Console.WriteLine();
                    Console.WriteLine(stackIndent + " Method: {0}",
                        sf.GetMethod() );
                    Console.WriteLine(  stackIndent + " File: {0}", 
                        sf.GetFileName());
                    Console.WriteLine(  stackIndent + " Line Number: {0}"
,
                        sf.GetFileLineNumber());
                    stackIndent += "  ";
                }
                throw e;
            }
        }
    }
}

/*
This console application produces the following output when
compiled with the Debug configuration.
  
   Method: Void ThrowsException()
   File: c:\samples\stacktraceframe\myclass.cs
   Line Number: 59

     Method: Void MyProtectedMethod()
     File: c:\samples\stacktraceframe\myclass.cs
     Line Number: 45

       Method: Void MyPublicMethod()
       File: c:\samples\stacktraceframe\myclass.cs
       Line Number: 39

         Method: Void Main(System.String[])
         File: c:\samples\stacktraceframe\myclass.cs
         Line Number: 13

  High up the call stack, Method: Void Main(System.String[])
  High up the call stack, Line Number: 20


This console application produces the following output when
compiled with the Release configuration.

   Method: Void ThrowsException()
   File:
   Line Number: 0

     Method: Void Main(System.String[])
     File:
     Line Number: 0

  High up the call stack, Method: Void Main(System.String[])
  High up the call stack, Line Number: 0

*/
#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
ref class StackTraceSample
{
private:
   ref class MyInternalClass
   {
   public:
      void ThrowsException()
      {
         try
         {
            throw gcnew Exception( "A problem was encountered." );
         }
         catch ( Exception^ e ) 
         {
            
            // Create a StackTrace that captures
            // filename, line number, and column
            // information for the current thread.
            StackTrace^ st = gcnew StackTrace( true );
            String^ stackIndent = "";
            for ( int i = 0; i < st->FrameCount;
 i++ )
            {
               
               // Note that at this level, there are five
               // stack frames, one for each method invocation.
               StackFrame^ sf = st->GetFrame( i );
               Console::WriteLine();
               Console::WriteLine( "{0}Method: {1}", stackIndent, sf->GetMethod()
 );
               Console::WriteLine( "{0}File: {1}", stackIndent, sf->GetFileName()
 );
               Console::WriteLine( "{0}Line Number: {1}", stackIndent,
 sf->GetFileLineNumber().ToString() );
               stackIndent = String::Concat( stackIndent, "  " );

            }
            throw e;
         }

      }

   };


protected:
   void MyProtectedMethod()
   {
      MyInternalClass^ mic = gcnew MyInternalClass;
      mic->ThrowsException();
   }


public:
   void MyPublicMethod()
   {
      MyProtectedMethod();
   }

};

int main()
{
   StackTraceSample^ sample = gcnew StackTraceSample;
   try
   {
      sample->MyPublicMethod();
   }
   catch ( Exception^ ) 
   {
      
      // Create a StackTrace that captures
      // filename, line number, and column
      // information for the current thread.
      StackTrace^ st = gcnew StackTrace( true );
      for ( int i = 0; i < st->FrameCount;
 i++ )
      {
         
         // For an executable built from C++, there
         // are two stack frames here: one for main,
         // and one for the _mainCRTStartup stub. 
         StackFrame^ sf = st->GetFrame( i );
         Console::WriteLine();
         Console::WriteLine( "High up the call stack, Method: {0}", sf->GetMethod()->ToString()
 );
         Console::WriteLine( "High up the call stack, Line Number: {0}",
 sf->GetFileLineNumber().ToString() );

      }
   }

}

/*
This console application produces the following output when
compiled with the Debug configuration.

  Method: Void ThrowsException()
  File: c:\samples\stacktraceframe\myclass.cpp
  Line Number: 20

    Method: Void MyProtectedMethod()
    File: c:\samples\stacktraceframe\myclass.cpp
    Line Number: 45

      Method: Void MyPublicMethod()
      File: c:\samples\stacktraceframe\myclass.cpp
      Line Number: 50
 
        Method: Int32 main()
        File: c:\samples\stacktraceframe\myclass.cpp
        Line Number: 56

          Method: UInt32 _mainCRTStartup()
          File:
          Line Number: 0

  High up the call stack, Method: Int32 main()
  High up the call stack, Line Number: 62

  High up the call stack, Method: UInt32 _mainCRTStartup()
  High up the call stack, Line Number: 0

This console application produces the following output when
compiled with the Release configuration.

  Method: Void ThrowsException()
  File:
  Line Number: 0

    Method: Int32 main()
    File:
    Line Number: 0

      Method: UInt32 _mainCRTStartup()
      File:
      Line Number: 0

  High up the call stack, Method: Int32 main()
  High up the call stack, Line Number: 0

  High up the call stack, Method: UInt32 _mainCRTStartup()
  High up the call stack, Line Number: 0

*/
import System.*;
import System.Diagnostics.*;

class StackTraceSample
{
    /** @attribute STAThread()
     */
    public static void main(String[]
 args)
    {
        StackTraceSample sample = new StackTraceSample();
        try {
            sample.MyPublicMethod();
        }
        catch (System.Exception exp) {
            // Create a StackTrace that captures
            // filename, line number, and column
            // information for the current thread.
            StackTrace st = new StackTrace(true);
            for (int i = 0; i < st.get_FrameCount();
 i++) {
                // Note that high up the call stack, there is only
                // one stack frame.
                StackFrame sf = st.GetFrame(i);
                Console.WriteLine();
                Console.WriteLine("High up the call stack, Method: {0}"
,
                    sf.GetMethod());
                Console.WriteLine("High up the call stack, Line Number: {0}"
,
                    (Int32)sf.GetFileLineNumber());
            }
        }
    } //main

    public void MyPublicMethod() throws System.Exception
    {
        MyProtectedMethod();
    } //MyPublicMethod

    protected void MyProtectedMethod() throws
 System.Exception
    {
        MyInternalClass mic = new MyInternalClass();
        mic.ThrowsException();
    } //MyProtectedMethod

    class MyInternalClass
    {
        public void ThrowsException() throws
 System.Exception
        {
            try {
                throw new System.Exception("A problem was
 encountered.");
            }
            catch (System.Exception e) {
                // Create a StackTrace that captures filename,
                // line number and column information.
                StackTrace st = new StackTrace(true);
                String stackIndent = "";
                for (int i = 0; i < st.get_FrameCount();
 i++) {
                    // Note that at this level, there are four
                    // stack frames, one for each method invocation.
                    StackFrame sf = st.GetFrame(i);
                    Console.WriteLine();
                    Console.WriteLine(stackIndent + " Method: {0}", 
                        sf.GetMethod());
                    Console.WriteLine(stackIndent + " File: {0}", 
                        sf.GetFileName());
                    Console.WriteLine(stackIndent + " Line Number: {0}",
 
                        (Int32)sf.GetFileLineNumber());
                    stackIndent += "  ";
                }
                throw e;
            }
        } //ThrowsException
    } //MyInternalClass
} //StackTraceSample

/*
This console application produces the following output when
compiled with the Debug configuration.
   
 Method: Void ThrowsException()
 File: c:\samples\stacktraceframe\myclass.jsl
 Line Number: 57

   Method: Void MyProtectedMethod()
   File: c:\samples\stacktraceframe\myclass.jsl
   Line Number: 43

     Method: Void MyPublicMethod()
     File: c:\samples\stacktraceframe\myclass.jsl
     Line Number: 37

       Method: Void main(System.String[])
       File: c:\samples\stacktraceframe\myclass.jsl
       Line Number: 14

High up the call stack, Method: Void main(System.String[])
High up the call stack, Line Number: 21

This console application produces the following output when
compiled with the Release configuration.

   
 Method: Void ThrowsException()
 File:
 Line Number: 0

   Method: Void MyProtectedMethod()
   File:
   Line Number: 0

     Method: Void MyPublicMethod()
     File:
     Line Number: 0

       Method: Void main(System.String[])
       File:
       Line Number: 0

High up the call stack, Method: Void main(System.String[])
High up the call stack, Line Number: 0

*/
継承階層継承階層
System.Object
  System.Diagnostics.StackTrace
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
StackTrace メンバ
System.Diagnostics 名前空間
Exception.StackTrace
Environment.StackTrace
ServerFault.StackTrace



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

辞書ショートカット

すべての辞書の索引

「StackTrace クラス」の関連用語

StackTrace クラスのお隣キーワード
検索ランキング

   

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



StackTrace クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS