StackTrace Class
Represents a stack trace, which is an ordered collection of one or more stack frames.
Assembly: mscorlib (in mscorlib.dll)
| Name | Description | |
|---|---|---|
![]() | StackTrace() | Initializes a new instance of the StackTrace class from the caller's frame. |
![]() | StackTrace(Boolean) | Initializes a new instance of the StackTrace class from the caller's frame, optionally capturing source information. |
![]() | StackTrace(Exception^) | Initializes a new instance of the StackTrace class using the provided exception object. |
![]() | StackTrace(Exception^, Boolean) | Initializes a new instance of the StackTrace class, using the provided exception object and optionally capturing source information. |
![]() | StackTrace(Exception^, Int32) | Initializes a new instance of the StackTrace class using the provided exception object and skipping the specified number of frames. |
![]() | 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(Int32) | Initializes a new instance of the StackTrace class from the caller's frame, skipping the specified number of frames. |
![]() | 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(StackFrame^) | Initializes a new instance of the StackTrace class that contains a single frame. |
![]() | StackTrace(Thread^, Boolean) | Obsolete. Initializes a new instance of the StackTrace class for a specific thread, optionally capturing source information. Do not use this constructor overload. |
| Name | Description | |
|---|---|---|
![]() | FrameCount | Gets the number of frames in the stack trace. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object^) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | Finalize() | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.) |
![]() | GetFrame(Int32) | Gets the specified stack frame. |
![]() | GetFrames() | Returns a copy of all stack frames in the current stack trace. |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetType() | |
![]() | MemberwiseClone() | |
![]() | ToString() | Builds a readable representation of the stack trace.(Overrides Object::ToString().) |
| Name | Description | |
|---|---|---|
![]() ![]() | METHODS_TO_SKIP | Defines the default for the number of methods to omit from the stack trace. This field is constant. |
StackTrace information will be most informative with Debug build configurations. By default, Debug builds include debug symbols, while Release builds do not. The debug symbols contain most of the file, method name, line number, and column information used in constructing StackFrame and StackTrace objects.
StackTrace might not report as many method calls as expected, due to code transformations that occur during optimization.
The following console application demonstrates how to create a simple StackTrace and iterate through its frames to obtain debugging and diagnostic information.
#using <System.dll> using namespace System; using namespace System::Diagnostics; ref class StackTraceSample { private: ref class MyInternalClass { public: void ThrowsException() { try { throw gcnew Exception( "A problem was encountered." ); } catch ( Exception^ e ) { // Create a StackTrace that captures // filename, line number, and column // information for the current thread. StackTrace^ st = gcnew StackTrace( true ); String^ stackIndent = ""; for ( int i = 0; i < st->FrameCount; i++ ) { // Note that at this level, there are five // stack frames, one for each method invocation. StackFrame^ sf = st->GetFrame( i ); Console::WriteLine(); Console::WriteLine( "{0}Method: {1}", stackIndent, sf->GetMethod() ); Console::WriteLine( "{0}File: {1}", stackIndent, sf->GetFileName() ); Console::WriteLine( "{0}Line Number: {1}", stackIndent, sf->GetFileLineNumber().ToString() ); stackIndent = String::Concat( stackIndent, " " ); } throw e; } } }; protected: void MyProtectedMethod() { MyInternalClass^ mic = gcnew MyInternalClass; mic->ThrowsException(); } public: void MyPublicMethod() { MyProtectedMethod(); } }; int main() { StackTraceSample^ sample = gcnew StackTraceSample; try { sample->MyPublicMethod(); } catch ( Exception^ ) { // Create a StackTrace that captures // filename, line number, and column // information for the current thread. StackTrace^ st = gcnew StackTrace( true ); for ( int i = 0; i < st->FrameCount; i++ ) { // For an executable built from C++, there // are two stack frames here: one for main, // and one for the _mainCRTStartup stub. StackFrame^ sf = st->GetFrame( i ); Console::WriteLine(); Console::WriteLine( "High up the call stack, Method: {0}", sf->GetMethod()->ToString() ); Console::WriteLine( "High up the call stack, Line Number: {0}", sf->GetFileLineNumber().ToString() ); } } } /* This console application produces the following output when compiled with the Debug configuration. Method: Void ThrowsException() File: c:\samples\stacktraceframe\myclass.cpp Line Number: 20 Method: Void MyProtectedMethod() File: c:\samples\stacktraceframe\myclass.cpp Line Number: 45 Method: Void MyPublicMethod() File: c:\samples\stacktraceframe\myclass.cpp Line Number: 50 Method: Int32 main() File: c:\samples\stacktraceframe\myclass.cpp Line Number: 56 Method: UInt32 _mainCRTStartup() File: Line Number: 0 High up the call stack, Method: Int32 main() High up the call stack, Line Number: 62 High up the call stack, Method: UInt32 _mainCRTStartup() High up the call stack, Line Number: 0 This console application produces the following output when compiled with the Release configuration. Method: Void ThrowsException() File: Line Number: 0 Method: Int32 main() File: Line Number: 0 Method: UInt32 _mainCRTStartup() File: Line Number: 0 High up the call stack, Method: Int32 main() High up the call stack, Line Number: 0 High up the call stack, Method: UInt32 _mainCRTStartup() High up the call stack, Line Number: 0 */
for the ability of inheritors to access unmanaged code. Associated enumeration: SecurityPermissionFlag::UnmanagedCode. This class cannot be inherited by partially trusted code.
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.




