The code is simple as follows:
Module Module1Module Module1
Public Sub ReportPosition () Sub ReportPosition (ByVal msg As String)
Dim strace As New StackTrace (True)
Dim frame As New StackFrame
Frame = strace. GetFrame (1) '0' indicates the innermost layer of the call stack (this function). 1 indicates that the previous layer is the function that calls ReportPosition,
MsgBox ("error message:" & msg & ControlChars. CrLf & "Location:" & frame. ToString)
'You can save the content of frame. ToString () to an object.
'You can call this method in trycatch to easily track abnormal locations.
End Sub
End Module
'Call
Private Sub button#click () Sub button#click (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles Button1.Click
Try
'Do something
Dim I As Int32 = 0
I = 1/I
Catch ex As Exception
ReportPosition (ex. Message)
End Try
End Sub
However, only in Debug mode and with A. pdb file can the file and row number of the function to be called be obtained.
What I thought of in the following article just now
Find out what's closing your application
The Code is as follows:
Private Sub form=closing () Sub form=closing (ByVal sender As Object ,_
ByVal e As System. ComponentModel. CancelEventArgs )_
Handles MyBase. Closing
Dim O As System. Diagnostics. StackTrace = _
New System. Diagnostics. StackTrace (True)
Dim F As System. Diagnostics. StackFrame
F = O. GetFrame (7)
Select Case F. GetMethod. Name. ToString
Case "SendMessage"
MsgBox ("Closing because of call in code .")
Case "CallWindowProc"
MsgBox ("Closing because of system menu click .")
Case "DispatchMessageW"
MsgBox ("Closing because of Task Manager .")
Case Else
MsgBox ("Don't Know why I'm closing !!?? ")
End Select
End Sub