StackTrace StackTrace StackTrace StackTrace Constructors

Definition

Overloads

StackTrace() StackTrace() StackTrace()

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

StackTrace(Boolean) StackTrace(Boolean) StackTrace(Boolean) StackTrace(Boolean)

Initializes a new instance of the StackTrace class from the caller's frame, optionally capturing source information.

StackTrace(StackFrame) StackTrace(StackFrame) StackTrace(StackFrame) StackTrace(StackFrame)

Initializes a new instance of the StackTrace class that contains a single frame.

StackTrace(Exception) StackTrace(Exception) StackTrace(Exception) StackTrace(Exception)

Initializes a new instance of the StackTrace class using the provided exception object.

StackTrace(Int32) StackTrace(Int32) StackTrace(Int32) StackTrace(Int32)

Initializes a new instance of the StackTrace class from the caller's frame, skipping the specified number of frames.

StackTrace(Exception, Int32) StackTrace(Exception, Int32) StackTrace(Exception, Int32) StackTrace(Exception, Int32)

Initializes a new instance of the StackTrace class using the provided exception object and skipping the specified number of frames.

StackTrace(Int32, Boolean) StackTrace(Int32, Boolean) StackTrace(Int32, Boolean) StackTrace(Int32, Boolean)

Initializes a new instance of the StackTrace class from the caller's frame, skipping the specified number of frames and optionally capturing source information.

StackTrace(Thread, Boolean) StackTrace(Thread, Boolean) StackTrace(Thread, Boolean) StackTrace(Thread, Boolean)

Initializes a new instance of the StackTrace class for a specific thread, optionally capturing source information.

Do not use this constructor overload.

StackTrace(Exception, Int32, Boolean) StackTrace(Exception, Int32, Boolean) StackTrace(Exception, Int32, Boolean) StackTrace(Exception, Int32, Boolean)

Initializes a new instance of the StackTrace class using the provided exception object, skipping the specified number of frames and optionally capturing source information.

StackTrace(Exception, Boolean) StackTrace(Exception, Boolean) StackTrace(Exception, Boolean) StackTrace(Exception, Boolean)

Initializes a new instance of the StackTrace class, using the provided exception object and optionally capturing source information.

StackTrace() StackTrace() StackTrace()

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

public:
 StackTrace();
public StackTrace ();
Public Sub New ()

Examples

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

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()
{
   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 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

Remarks

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.

StackTrace(Boolean) StackTrace(Boolean) StackTrace(Boolean) StackTrace(Boolean)

Initializes a new instance of the StackTrace class from the caller's frame, optionally capturing source information.

public:
 StackTrace(bool fNeedFileInfo);
public StackTrace (bool fNeedFileInfo);
new System.Diagnostics.StackTrace : bool -> System.Diagnostics.StackTrace
Public Sub New (fNeedFileInfo As Boolean)

Parameters

fNeedFileInfo
Boolean Boolean Boolean Boolean

true to capture the file name, line number, and column number; otherwise, false.

Examples

The following code example demonstrates various StackTrace constructor methods.

void Level2Method()
{
   try
   {
      ClassLevel3^ nestedClass = gcnew ClassLevel3;
      nestedClass->Level3Method();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( " Level2Method exception handler" );
      
      // Display the full call stack at this level.
      StackTrace^ st1 = gcnew StackTrace( true );
      Console::WriteLine( " Stack trace for this level: {0}", st1->ToString() );
      
      // Build a stack trace from one frame, skipping the
      // current frame and using the next frame.
      StackTrace^ st2 = gcnew StackTrace( gcnew StackFrame( 1,true ) );
      Console::WriteLine( " Stack trace built with next level frame: {0}", st2->ToString() );
      
      // Build a stack trace skipping the current frame, and
      // including all the other frames.
      StackTrace^ st3 = gcnew StackTrace( 1,true );
      Console::WriteLine( " Stack trace built from the next level up: {0}", st3->ToString() );
      Console::WriteLine();
      Console::WriteLine( "   ... throwing exception to next level ..." );
      Console::WriteLine( "-------------------------------------------------\n" );
      throw e;
   }

}

public void Level2Method()
{
   try 
   {
      ClassLevel3 nestedClass = new ClassLevel3();
      nestedClass.Level3Method();

   }
   catch (Exception e) 
   {
      Console.WriteLine(" Level2Method exception handler");

      // Display the full call stack at this level.
      StackTrace st1 = new StackTrace(true);
      Console.WriteLine(" Stack trace for this level: {0}",
         st1.ToString());

      // Build a stack trace from one frame, skipping the current
      // frame and using the next frame.
      StackTrace st2 = new StackTrace(new StackFrame(1, true));
      Console.WriteLine(" Stack trace built with next level frame: {0}",
         st2.ToString());

      // Build a stack trace skipping the current frame, and
      // including all the other frames.
      StackTrace st3 = new StackTrace(1, true);
      Console.WriteLine(" Stack trace built from the next level up: {0}",
         st3.ToString());

      Console.WriteLine();
      Console.WriteLine("   ... throwing exception to next level ...");
      Console.WriteLine("-------------------------------------------------\n");
      throw e;
   }
}
Public Sub Level2Method()
   Try
      Dim nestedClass As New ClassLevel3
      nestedClass.Level3Method()
   
   Catch e As Exception
      Console.WriteLine(" Level2Method exception handler")
      
      ' Display the full call stack at this level.
      Dim st1 As New StackTrace(True)
      Console.WriteLine(" Stack trace for this level: {0}", _
         st1.ToString())
      
      ' Build a stack trace from one frame, skipping the current
      ' frame and using the next frame.
      Dim st2 As New StackTrace(New StackFrame(1, True))
      Console.WriteLine(" Stack trace built with next level frame: {0}", _
          st2.ToString())
      
      ' Build a stack trace skipping the current frame, and
      ' including all the other frames.
      Dim st3 As New StackTrace(1, True)
      Console.WriteLine(" Stack trace built from the next level up: {0}", _
          st3.ToString())
      
      Console.WriteLine()
      Console.WriteLine("   ... throwing exception to next level ...")
      Console.WriteLine("-------------------------------------------------")
      Console.WriteLine()
      Throw e
   End Try
End Sub 'Level2Method

Remarks

The StackTrace is created with the caller's current thread.

StackTrace(StackFrame) StackTrace(StackFrame) StackTrace(StackFrame) StackTrace(StackFrame)

Initializes a new instance of the StackTrace class that contains a single frame.

public:
 StackTrace(System::Diagnostics::StackFrame ^ frame);
public StackTrace (System.Diagnostics.StackFrame frame);
new System.Diagnostics.StackTrace : System.Diagnostics.StackFrame -> System.Diagnostics.StackTrace
Public Sub New (frame As StackFrame)

Parameters

frame
StackFrame StackFrame StackFrame StackFrame

The frame that the StackTrace object should contain.

Examples

The following code example writes stack trace information to an event log entry.

StackFrame^ fr = gcnew StackFrame( 1,true );
StackTrace^ st = gcnew StackTrace( fr );
EventLog::WriteEntry( fr->GetMethod()->Name, st->ToString(), EventLogEntryType::Warning );
StackFrame fr = new StackFrame(1,true);
StackTrace st = new StackTrace(fr);
EventLog.WriteEntry(fr.GetMethod().Name,
                    st.ToString(),
                    EventLogEntryType.Warning);
Dim frame As New StackFrame(1, True)
Dim strace As New StackTrace(frame)            

EventLog.WriteEntry(frame.GetMethod().Name, _
                    strace.ToString(), _
                    EventLogEntryType.Warning)

Remarks

Use this constructor when you do not want the overhead of a full stack trace.

See also

StackTrace(Exception) StackTrace(Exception) StackTrace(Exception) StackTrace(Exception)

Initializes a new instance of the StackTrace class using the provided exception object.

public:
 StackTrace(Exception ^ e);
public StackTrace (Exception e);
new System.Diagnostics.StackTrace : Exception -> System.Diagnostics.StackTrace
Public Sub New (e As Exception)

Parameters

e
Exception Exception Exception Exception

The exception object from which to construct the stack trace.

Exceptions

Remarks

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

The resulting stack trace describes the stack at the time of the exception.

See also

StackTrace(Int32) StackTrace(Int32) StackTrace(Int32) StackTrace(Int32)

Initializes a new instance of the StackTrace class from the caller's frame, skipping the specified number of frames.

public:
 StackTrace(int skipFrames);
public StackTrace (int skipFrames);
new System.Diagnostics.StackTrace : int -> System.Diagnostics.StackTrace
Public Sub New (skipFrames As Integer)

Parameters

skipFrames
Int32 Int32 Int32 Int32

The number of frames up the stack from which to start the trace.

Exceptions

Remarks

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

If the number of frames to skip is greater than or equal to the total number of frames on the call stack at the time the instance is created, the StackTrace will contain no frames.

StackTrace(Exception, Int32) StackTrace(Exception, Int32) StackTrace(Exception, Int32) StackTrace(Exception, Int32)

Initializes a new instance of the StackTrace class using the provided exception object and skipping the specified number of frames.

public:
 StackTrace(Exception ^ e, int skipFrames);
public StackTrace (Exception e, int skipFrames);
new System.Diagnostics.StackTrace : Exception * int -> System.Diagnostics.StackTrace
Public Sub New (e As Exception, skipFrames As Integer)

Parameters

e
Exception Exception Exception Exception

The exception object from which to construct the stack trace.

skipFrames
Int32 Int32 Int32 Int32

The number of frames up the stack from which to start the trace.

Exceptions

Remarks

The StackTrace does not contain file name, line number, or column information.

The resulting stack trace describes the stack at the time of the exception.

If the number of frames to skip is greater than or equal to the total number of frames on the call stack at the time the instance is created, the StackTrace will contain no frames.

See also

StackTrace(Int32, Boolean) StackTrace(Int32, Boolean) StackTrace(Int32, Boolean) StackTrace(Int32, Boolean)

Initializes a new instance of the StackTrace class from the caller's frame, skipping the specified number of frames and optionally capturing source information.

public:
 StackTrace(int skipFrames, bool fNeedFileInfo);
public StackTrace (int skipFrames, bool fNeedFileInfo);
new System.Diagnostics.StackTrace : int * bool -> System.Diagnostics.StackTrace
Public Sub New (skipFrames As Integer, fNeedFileInfo As Boolean)

Parameters

skipFrames
Int32 Int32 Int32 Int32

The number of frames up the stack from which to start the trace.

fNeedFileInfo
Boolean Boolean Boolean Boolean

true to capture the file name, line number, and column number; otherwise, false.

Exceptions

Examples

The following code example demonstrates various StackTrace constructor methods.

void Level2Method()
{
   try
   {
      ClassLevel3^ nestedClass = gcnew ClassLevel3;
      nestedClass->Level3Method();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( " Level2Method exception handler" );
      
      // Display the full call stack at this level.
      StackTrace^ st1 = gcnew StackTrace( true );
      Console::WriteLine( " Stack trace for this level: {0}", st1->ToString() );
      
      // Build a stack trace from one frame, skipping the
      // current frame and using the next frame.
      StackTrace^ st2 = gcnew StackTrace( gcnew StackFrame( 1,true ) );
      Console::WriteLine( " Stack trace built with next level frame: {0}", st2->ToString() );
      
      // Build a stack trace skipping the current frame, and
      // including all the other frames.
      StackTrace^ st3 = gcnew StackTrace( 1,true );
      Console::WriteLine( " Stack trace built from the next level up: {0}", st3->ToString() );
      Console::WriteLine();
      Console::WriteLine( "   ... throwing exception to next level ..." );
      Console::WriteLine( "-------------------------------------------------\n" );
      throw e;
   }

}

public void Level2Method()
{
   try 
   {
      ClassLevel3 nestedClass = new ClassLevel3();
      nestedClass.Level3Method();

   }
   catch (Exception e) 
   {
      Console.WriteLine(" Level2Method exception handler");

      // Display the full call stack at this level.
      StackTrace st1 = new StackTrace(true);
      Console.WriteLine(" Stack trace for this level: {0}",
         st1.ToString());

      // Build a stack trace from one frame, skipping the current
      // frame and using the next frame.
      StackTrace st2 = new StackTrace(new StackFrame(1, true));
      Console.WriteLine(" Stack trace built with next level frame: {0}",
         st2.ToString());

      // Build a stack trace skipping the current frame, and
      // including all the other frames.
      StackTrace st3 = new StackTrace(1, true);
      Console.WriteLine(" Stack trace built from the next level up: {0}",
         st3.ToString());

      Console.WriteLine();
      Console.WriteLine("   ... throwing exception to next level ...");
      Console.WriteLine("-------------------------------------------------\n");
      throw e;
   }
}
Public Sub Level2Method()
   Try
      Dim nestedClass As New ClassLevel3
      nestedClass.Level3Method()
   
   Catch e As Exception
      Console.WriteLine(" Level2Method exception handler")
      
      ' Display the full call stack at this level.
      Dim st1 As New StackTrace(True)
      Console.WriteLine(" Stack trace for this level: {0}", _
         st1.ToString())
      
      ' Build a stack trace from one frame, skipping the current
      ' frame and using the next frame.
      Dim st2 As New StackTrace(New StackFrame(1, True))
      Console.WriteLine(" Stack trace built with next level frame: {0}", _
          st2.ToString())
      
      ' Build a stack trace skipping the current frame, and
      ' including all the other frames.
      Dim st3 As New StackTrace(1, True)
      Console.WriteLine(" Stack trace built from the next level up: {0}", _
          st3.ToString())
      
      Console.WriteLine()
      Console.WriteLine("   ... throwing exception to next level ...")
      Console.WriteLine("-------------------------------------------------")
      Console.WriteLine()
      Throw e
   End Try
End Sub 'Level2Method

Remarks

If the number of frames to skip is greater than or equal to the total number of frames on the call stack at the time the instance is created, the StackTrace will contain no frames.

StackTrace(Thread, Boolean) StackTrace(Thread, Boolean) StackTrace(Thread, Boolean) StackTrace(Thread, Boolean)

Warning

This API is now obsolete.

Initializes a new instance of the StackTrace class for a specific thread, optionally capturing source information.

Do not use this constructor overload.

public:
 StackTrace(System::Threading::Thread ^ targetThread, bool needFileInfo);
[System.Obsolete]
[System.Obsolete("This constructor has been deprecated.  Please use a constructor that does not require a Thread parameter.  http://go.microsoft.com/fwlink/?linkid=14202")]
public StackTrace (System.Threading.Thread targetThread, bool needFileInfo);
new System.Diagnostics.StackTrace : System.Threading.Thread * bool -> System.Diagnostics.StackTrace
Public Sub New (targetThread As Thread, needFileInfo As Boolean)

Parameters

targetThread
Thread Thread Thread Thread

The thread whose stack trace is requested.

needFileInfo
Boolean Boolean Boolean Boolean

true to capture the file name, line number, and column number; otherwise, false.

Exceptions

Remarks

Important

Do not use this constructor. It is obsolete, and there is no recommended alternative. When you suspend a thread, you have no way of knowing what code it is executing, and deadlocks can occur very easily. For example, if you suspend a thread while it holds locks during a security permission evaluation, other threads in the AppDomain might be blocked. If you suspend a thread while it is executing a class constructor, other threads in the AppDomain that attempt to use that class are blocked.

See also

StackTrace(Exception, Int32, Boolean) StackTrace(Exception, Int32, Boolean) StackTrace(Exception, Int32, Boolean) StackTrace(Exception, Int32, Boolean)

Initializes a new instance of the StackTrace class using the provided exception object, skipping the specified number of frames and optionally capturing source information.

public:
 StackTrace(Exception ^ e, int skipFrames, bool fNeedFileInfo);
public StackTrace (Exception e, int skipFrames, bool fNeedFileInfo);
new System.Diagnostics.StackTrace : Exception * int * bool -> System.Diagnostics.StackTrace
Public Sub New (e As Exception, skipFrames As Integer, fNeedFileInfo As Boolean)

Parameters

e
Exception Exception Exception Exception

The exception object from which to construct the stack trace.

skipFrames
Int32 Int32 Int32 Int32

The number of frames up the stack from which to start the trace.

fNeedFileInfo
Boolean Boolean Boolean Boolean

true to capture the file name, line number, and column number; otherwise, false.

Exceptions

Remarks

The resulting stack trace describes the stack at the time of the exception.

If the number of frames to skip is greater than or equal to the total number of frames on the call stack at the time the instance is created, the StackTrace will contain no frames.

See also

StackTrace(Exception, Boolean) StackTrace(Exception, Boolean) StackTrace(Exception, Boolean) StackTrace(Exception, Boolean)

Initializes a new instance of the StackTrace class, using the provided exception object and optionally capturing source information.

public:
 StackTrace(Exception ^ e, bool fNeedFileInfo);
public StackTrace (Exception e, bool fNeedFileInfo);
new System.Diagnostics.StackTrace : Exception * bool -> System.Diagnostics.StackTrace
Public Sub New (e As Exception, fNeedFileInfo As Boolean)

Parameters

exception
Exception Exception Exception Exception

The exception object from which to construct the stack trace.

needFileInfo
Boolean Boolean Boolean Boolean

true to capture the file name, line number, and column number; otherwise, false.

Exceptions

Remarks

The resulting stack trace describes the stack at the time of the exception.

See also

Applies to