StackTrace Constructor ()

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Initializes a new instance of the StackTrace class from the caller's frame.

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

public StackTrace()

The StackTrace is created with the caller's current thread, and does not contain file name, line number, or column information.

Use this default constructor when you want a complete trace with only summary method information about the call stack.

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: