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?
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 aTry/Catch
statement, but you can also get it from unhandled exceptions via theAppDomain.UnhandledException
event: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:
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 theCustomClass.vb
file. It also reveals that thePerformAction
method was called by theMainForm.CheckStatus
method, which in turn was (presumably) called by clickingButton1
.When you get an error message as shown below click on the View Detail.. link.
This will open this property box.
Expand the exception to show the details and hover the mouse over the StackTrace property.
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
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.
Also when running the exe version be familiar with the details pane. Its the same stack-trace.