StackTrace.GetFrame Method (Int32)

 

Gets the specified stack frame.

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

public virtual StackFrame GetFrame(
	int index
)

Parameters

index
Type: System.Int32

The index of the stack frame requested.

Return Value

Type: System.Diagnostics.StackFrame

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

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show: