The Stack Trace of an error can be accessed as a string via the StackTrace
property of any exception. Mainly you'd get it in from the exception caught in a Try/Catch
statement, but you can also get it from unhandled exceptions via the AppDomain.UnhandledException
event:
'Somewhere in your code, preferrably at startup.
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AppDomain_UnhandledException
'The event handler.
Private Sub AppDomain_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs)
Dim ex As Exception = DirectCast(e.ExceptionObject, Exception) 'Cast the ExceptionObject into an Exception.
MessageBox.Show("An unhandled exception occurred. Stack Trace:" & Environment.NewLine & ex.StackTrace)
End Sub
The Stack Trace tells you where an exception occurred and also which methods was called in order to reach that point. If you are debugging on your own machine the Stack Trace will also reveal file names and line numbers of the calls.
An example of a Stack Trace could be:
at MyProject.CustomClass.PerformActions(Int32 ID) in C:\Projects\MyProject\CustomClass.vb:line 12
at MyProject.MainForm.CheckStatus() in C:\Projects\MyProject\MainForm.vb:line 65
at MyProject.MainForm.Button1_Click(Object sender, EventArgs e) at C:\Projects\MyProject\MainForm.vb:line 33
Each line of the Stack Trace represents a method that has been called in the current call hierarchy. The first line represents the latest/current method called.
The above stack trace example tells you that the error occurred at line 12 in the PerformActions
method of the CustomClass.vb
file. It also reveals that the PerformAction
method was called by the MainForm.CheckStatus
method, which in turn was (presumably) called by clicking Button1
.