How to access and use the stack-trace to help iden

2019-09-22 01:13发布

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?

2条回答
Viruses.
2楼-- · 2019-09-22 01:41

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.

查看更多
时光不老,我们不散
3楼-- · 2019-09-22 01:56

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

查看更多
登录 后发表回答