次の方法で共有


StackTrace コンストラクタ ()

StackTrace クラスの新しいインスタンスを呼び出し元のフレームから初期化します。

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

構文

'宣言
Public Sub New
'使用
Dim instance As New StackTrace
public StackTrace ()
public:
StackTrace ()
public StackTrace ()
public function StackTrace ()

解説

StackTrace は、呼び出し元の現在のスレッドで作成され、ファイル名、行番号、および列情報を含みません。

コール スタックに関するメソッドの概要情報だけを持つ完全なトレースが必要な場合は、この既定のコンストラクタを使用します。

使用例

スタック トレース内の最初と最後の関数呼び出しを表示するコード例を次に示します。

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
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;
   }        
}
void Level5Method()
{
   try
   {
      ClassLevel6^ nestedClass = gcnew ClassLevel6;
      nestedClass->Level6Method();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( " Level5Method exception handler" );
      StackTrace^ st = gcnew 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 void Level5Method() throws System.Exception
{
    try {
        ClassLevel6 nestedClass = new ClassLevel6();
        nestedClass.Level6Method();
    }
    catch (System.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.get_FrameCount() > 1) {
            // Display the highest-level function call 
            // in the trace.
            sf = st.GetFrame(st.get_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;
    }
} //Level5Method

プラットフォーム

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

参照

関連項目

StackTrace クラス
StackTrace メンバ
System.Diagnostics 名前空間