Use StackTrace in the called function to determine which function is called.

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.