Click to See Complete Forum and Search --> : [RESOLVED] What Sub/Function/Block of Code Produced Error
fallnwrld
March 2nd, 2010, 12:03 AM
I'm creating some software in visual basic 2005 .Net. My software keeps a log file. If the applications encounters an error, it writes to this log file. Is there a way I can easily produce what sub, function, or block of code that produced the error?
Example:
Sub MessageUser()
Try
msbox("Hello")
Catch
msgbox("I want to be able to know MessageUser is the sub that encountered the error.")
End try
End Sub
Since msgbox is spelled incorrectly this will produce an error. I want to know what sub caused the error, in this case MessageUser.
dlarkin77
March 2nd, 2010, 07:04 AM
Try this
Public Shared Function GetCurrentMethodName() As String
Dim frame As New StackFrame(1, True)
Return frame.GetMethod().ReflectedType.Name & "." & frame.GetMethod().Name
End Function
Sub MessageUser()
Try
msbox("Hello")
Catch
Dim message As String = "Error occurred in : " & GetCurrentMethodName()
msgbox(message)
End try
You will need to add an Import statement for System.Diagnostics
PeejAvery
March 2nd, 2010, 07:54 AM
Also, remember that you are using VB.NET. You should be using MessageBox.Show(...) and not MsgBox(...).
fallnwrld
March 2nd, 2010, 07:58 AM
Try this
Public Shared Function GetCurrentMethodName() As String
Dim frame As New StackFrame(1, True)
Return frame.GetMethod().ReflectedType.Name & "." & frame.GetMethod().Name
End Function
Sub MessageUser()
Try
msbox("Hello")
Catch
Dim message As String = "Error occurred in : " & GetCurrentMethodName()
msgbox(message)
End try
You will need to add an Import statement for System.Diagnostics
Thanks for your help. I provides me the name of the method that called the sub, but not the sub itself. It gave me the load method of the form, which is the method that actually calls it. Anyway to get the name of the sub? That will be a lot more specific and helpful.
HanneSThEGreaT
March 2nd, 2010, 08:30 AM
Try this :
Imports System
Imports System.Reflection
Public Class Form1
Private Sub GetCurrentMethodName()
Dim sf As StackFrame = New StackFrame()
Dim mb As MethodBase = sf.GetMethod()
MessageBox.Show(mb.Name)
End Sub
Private Sub WhoCalledTheMethod()
Dim st As StackTrace = New StackTrace()
Dim sf As StackFrame = st.GetFrame(1)
Dim mb As MethodBase = sf.GetMethod()
MessageBox.Show("I was called by: " & mb.Name)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
GetCurrentMethodName()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
WhoCalledTheMethod()
End Sub
Button 1 will show "GetCurrentMethodName" inside a messagebox, which is the current executing method.
Button 2 will show "Button2_Click()", the event that called the procedure.
You must make sure to include the Import for System.Reflection, else your MethodBase will not work :)
I hope it helps! :D
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.