-2

I see a lot of posts from people saying they get error such and such but seem to have no idea how to figure out where the error is coming from.

As such I wondered if people know how to access and use the stack trace.

So how do you access it, and what does it mean and do for you?

Trevor_G
  • 1,331
  • 1
  • 8
  • 17
  • 5
    Numerous duplicates exist: [How to use Stacktrace to return Error Line Number](http://stackoverflow.com/q/13460656/1070452), [Getting VB.NET line numbers in stack trace](http://stackoverflow.com/q/14047821/1070452), [How to get a stack trace in .NET in normal execution](http://stackoverflow.com/q/3259482/1070452) – Ňɏssa Pøngjǣrdenlarp Feb 09 '17 at 17:56
  • 3
    Wouldn't this be a better fit for Documentation? – Tim Feb 09 '17 at 17:57
  • People don't bother to search previous posts (though I though I did..)much less read the documentation section...... – Trevor_G Feb 09 '17 at 18:05
  • 1
    No-one so far seems to have mentioned the Call Stack window at the bottom of the IDE. – peterG Feb 09 '17 at 18:47
  • 2
    If they don't bother to search how does another version help? – user3697824 Feb 09 '17 at 19:49
  • @user3697824 : Perhaps he wants to use it for duplicates? His question and answer are focusing more on the stack trace as a whole than what Plutonix's links are (with an exception to the final link). – Visual Vincent Feb 09 '17 at 20:19
  • Possible duplicate of [How to get a stack trace in .NET in normal execution?](https://stackoverflow.com/questions/3259482/how-to-get-a-stack-trace-in-net-in-normal-execution) –  Dec 01 '17 at 01:13

2 Answers2

5

When you get an error message as shown below click on the View Detail.. link.

Error Report

This will open this property box.

Expand the exception to show the details and hover the mouse over the StackTrace property.

Error Details

Most of what you see listed there will probably be Greek to you (Apologies to the Grecian friends reading this), but if you look closer you will see lines that say

at "NAME OF A MODULE":Line 230 (or wherever)

The first line like that in the trace is the line that finally crashed the code. Simply go find that line in your code and try and figure out why that particular line is causing the specified error.

Subsequent lines of that format are the point in your code where the routine listed above was called from.

Now the images above are not a very good example since the IDE debugger stopped at the error line. But regardless, the stack-trace in invaluable when you properly trap errors in a try / catch statement. One of the properties of the exception is the stack-trace.. view it or dump it to the immediate window.

    Try

    Catch ex As Exception
        Debug.Print(ex.StackTrace)
    End Try

Also when running the exe version be familiar with the details pane. Its the same stack-trace.

enter image description here

Trevor_G
  • 1,331
  • 1
  • 8
  • 17
1

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.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75