This documentation is archived and is not being maintained.

StackTrace.GetFrame Method

Gets the specified stack frame.

Namespace: System.Diagnostics
Assembly: mscorlib (in mscorlib.dll)

public virtual StackFrame GetFrame (
	int index
)
public StackFrame GetFrame (
	int index
)
public function GetFrame (
	index : int
) : StackFrame

Parameters

index

The index of the stack frame requested.

Return Value

The specified stack frame.

Stack frames are numbered starting at 0, which is the last stack frame pushed.

The following code example displays the first and last function calls in a stack trace.

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 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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Show: