This topic has not yet been rated - Rate this topic

StackTrace Constructor

.NET Framework 1.1

Initializes a new instance of the StackTrace class.

Overload List

Initializes a new instance of the StackTrace class from the caller's frame. The StackTrace is created with the caller's current thread, and does not contain file name, line number, or column information.

[Visual Basic] Public Sub New()
[C#] public StackTrace();
[C++] public: StackTrace();
[JScript] public function StackTrace();

Initializes a new instance of the StackTrace class from the caller's frame, optionally capturing source information. The StackTrace is created with the caller's current thread.

[Visual Basic] Public Sub New(Boolean)
[C#] public StackTrace(bool);
[C++] public: StackTrace(bool);
[JScript] public function StackTrace(Boolean);

Initializes a new instance of the StackTrace class.

[Visual Basic] Public Sub New(Exception)
[C#] public StackTrace(Exception);
[C++] public: StackTrace(Exception*);
[JScript] public function StackTrace(Exception);

Initializes a new instance of the StackTrace class from the caller's frame, skipping the given number of frames. The StackTrace is created with the caller's current thread, and does not contain file name, line number, or column information.

[Visual Basic] Public Sub New(Integer)
[C#] public StackTrace(int);
[C++] public: StackTrace(int);
[JScript] public function StackTrace(int);

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

[Visual Basic] Public Sub New(StackFrame)
[C#] public StackTrace(StackFrame);
[C++] public: StackTrace(StackFrame*);
[JScript] public function StackTrace(StackFrame);

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

[Visual Basic] Public Sub New(Exception, Boolean)
[C#] public StackTrace(Exception, bool);
[C++] public: StackTrace(Exception*, bool);
[JScript] public function StackTrace(Exception, Boolean);

Initializes a new instance of the StackTrace class using the provided exception object and optionally skipping the given number of frames.The StackTrace does not contain file name, line number, or column information.

[Visual Basic] Public Sub New(Exception, Integer)
[C#] public StackTrace(Exception, int);
[C++] public: StackTrace(Exception*, int);
[JScript] public function StackTrace(Exception, int);

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

[Visual Basic] Public Sub New(Integer, Boolean)
[C#] public StackTrace(int, bool);
[C++] public: StackTrace(int, bool);
[JScript] public function StackTrace(int, Boolean);

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

[Visual Basic] Public Sub New(Thread, Boolean)
[C#] public StackTrace(Thread, bool);
[C++] public: StackTrace(Thread*, bool);
[JScript] public function StackTrace(Thread, Boolean);

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

[Visual Basic] Public Sub New(Exception, Integer, Boolean)
[C#] public StackTrace(Exception, int, bool);
[C++] public: StackTrace(Exception*, int, bool);
[JScript] public function StackTrace(Exception, int, Boolean);

Example

[Visual Basic, C#, C++] The following code example demonstrates how a StackTrace instantiated with this constructor can be used.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the StackTrace constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
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

[C#] 
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;
   }
}

[C++] 
void Level2Method()
{
   try 
   {
      ClassLevel3 *nestedClass = new ClassLevel3();
      nestedClass->Level3Method();

   }
   catch (Exception *e) 
   {
      Console::WriteLine(S" Level2Method exception handler");

      // Display the full call stack at this level.
      StackTrace *st1 = new StackTrace(true);
      Console::WriteLine(S" 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(S" 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(S" Stack trace built from the next level up: {0}", 
         st3->ToString());

      Console::WriteLine();
      Console::WriteLine(S"   ... throwing exception to next level ...");
      Console::WriteLine(S"-------------------------------------------------\n");
      throw e;
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

StackTrace Class | StackTrace Members | System.Diagnostics Namespace

Did you find this helpful?
(1500 characters remaining)