|
-
March 2nd, 2010, 01:03 AM
#1
[RESOLVED] What Sub/Function/Block of Code Produced Error
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.
Using VB .Net 2008 Express Edition
-
March 2nd, 2010, 08:04 AM
#2
Re: What Sub/Function/Block of Code Produced Error
Try this
Code:
Public Shared Function GetCurrentMethodName() As String
Dim frame As New StackFrame(1, True)
Return frame.GetMethod().ReflectedType.Name & "." & frame.GetMethod().Name
End Function
Code:
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
Last edited by dlarkin77; March 2nd, 2010 at 08:09 AM.
-
March 2nd, 2010, 08:54 AM
#3
Re: What Sub/Function/Block of Code Produced Error
Also, remember that you are using VB.NET. You should be using MessageBox.Show(...) and not MsgBox(...).
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 2nd, 2010, 08:58 AM
#4
Re: What Sub/Function/Block of Code Produced Error
 Originally Posted by dlarkin77
Try this
Code:
Public Shared Function GetCurrentMethodName() As String
Dim frame As New StackFrame(1, True)
Return frame.GetMethod().ReflectedType.Name & "." & frame.GetMethod().Name
End Function
Code:
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.
Using VB .Net 2008 Express Edition
-
March 2nd, 2010, 09:30 AM
#5
Re: What Sub/Function/Block of Code Produced Error
Last edited by HanneSThEGreaT; March 2nd, 2010 at 09:33 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|