Export (0) Print
Expand All

Exception.StackTrace Property

Updated: September 2009

Gets a string representation of the frames on the call stack at the time the current exception was thrown.

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

'Declaration
Public Overridable ReadOnly Property StackTrace As String
'Usage
Dim instance As Exception 
Dim value As String 

value = instance.StackTrace

Property Value

Type: System.String
A string that describes the immediate frames of the call stack.

Implements

_Exception.StackTrace

The execution stack keeps track of all the methods that are in execution at a given instant. A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs.

The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown. You can obtain information about additional frames in the call stack by creating a new instance of the StackTrace class and using its ToString method.

The common language runtime (CLR) updates the stack trace whenever an exception is thrown in application code (by using the throw keyword). If the exception was rethrown in a method that is different than the method where it was originally thrown, the stack trace contains both the location in the method where the exception was originally thrown, and the location in the method where the exception was rethrown. If the exception is thrown, and later rethrown, in the same method, the stack trace only contains the location where the exception was rethrown and does not include the location where the exception was originally thrown.

The StackTrace property may not report as many method calls as expected because of code transformations, such as inlining, that occur during optimization.

Notes to Inheritors:

The StackTrace property is overridden in classes that require control over the stack trace content or format.

By default, the stack trace is captured immediately before an exception object is thrown. Use Environment.StackTrace to get stack trace information when no exception is being thrown.

The following code example throws an Exception and then catches it and displays a stack trace using the StackTrace property.

' Example for the Exception.HelpLink, Exception.Source, 
' Exception.StackTrace, and Exception.TargetSite properties. 
Imports System
Imports Microsoft.VisualBasic

Namespace NDP_UE_VB

    ' Derive an exception; the constructor sets the HelpLink and  
    ' Source properties. 
    Class LogTableOverflowException
        Inherits Exception

        Private Const overflowMessage As String = _
            "The log table has overflowed." 

        Public Sub New( auxMessage As String, inner As Exception )
            MyBase.New( String.Format( "{0} - {1}", _
                overflowMessage, auxMessage ), inner )

            Me.HelpLink = "http://msdn.microsoft.com" 
            Me.Source = "Exception_Class_Samples" 

        End Sub ' New 
    End Class ' LogTableOverflowException

    Class LogTable

        Public Sub New(numElements As Integer)
            logArea = New String(numElements) {}
            elemInUse = 0
        End Sub ' New 

        Protected logArea() As String 
        Protected elemInUse As Integer 

        ' The AddRecord method throws a derived exception if  
        ' the array bounds exception is caught. 
        Public Function AddRecord( newRecord As String ) As Integer 

            Try 
                Dim curElement as Integer = elemInUse
                logArea( elemInUse ) = newRecord
                elemInUse += 1
                Return curElement

            Catch ex As Exception
                Throw New LogTableOverflowException( _
                    String.Format( "Record ""{0}"" was not logged.", _
                        newRecord ), ex )
            End Try 
        End Function ' AddRecord
    End Class ' LogTable

    Module OverflowDemo

        ' Create a log table and force an overflow. 
        Sub Main( )
            Dim log As New LogTable( 4 )

            Console.WriteLine( "This example of " & vbCrLf & _
                "   Exception.Message, " & vbCrLf & _
                "   Exception.HelpLink, " & vbCrLf & _
                "   Exception.Source, " & vbCrLf & _
                "   Exception.StackTrace, and " & vbCrLf & _
                "   Exception.TargetSite " & vbCrLf & _
                "generates the following output." )

            Try 
                Dim count As Integer = 0

                Do
                    log.AddRecord( _
                        String.Format( "Log record number {0}", count ) )
                    count += 1
                Loop 

            Catch ex As Exception
                Console.WriteLine( vbCrLf & _
                    "Message ---" & vbCrLf & ex.Message )
                Console.WriteLine( vbCrLf & _
                    "HelpLink ---" & vbCrLf & ex.HelpLink )
                Console.WriteLine( vbCrLf & _
                    "Source ---" & vbCrLf & ex.Source )
                Console.WriteLine( vbCrLf & _
                    "StackTrace ---" & vbCrLf & ex.StackTrace )
                Console.WriteLine( vbCrLf & "TargetSite ---" & _
                    vbCrLf & ex.TargetSite.ToString( ) )
            End Try 
        End Sub ' Main

    End Module ' OverflowDemo
End Namespace ' NDP_UE_VB

' This example of 
'    Exception.Message, 
'    Exception.HelpLink, 
'    Exception.Source, 
'    Exception.StackTrace, and 
'    Exception.TargetSite 
' generates the following output. 
'  
' Message --- 
' The log table has overflowed. - Record "Log record number 5" was not logged. 
'  
' HelpLink --- 
' http://msdn.microsoft.com 
'  
' Source --- 
' Exception_Class_Samples 
'  
' StackTrace --- 
'    at NDP_UE_VB.LogTable.AddRecord(String newRecord) 
'    at NDP_UE_VB.OverflowDemo.Main() 
'  
' TargetSite --- 
' Int32 AddRecord(System.String)

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

September 2009

Clarified remarks

Information enhancement.

Community Additions

ADD
Show:
© 2014 Microsoft