CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2003
    Posts
    95

    [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

  2. #2
    Join Date
    Oct 2004
    Posts
    206

    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.

  3. #3
    Join Date
    May 2002
    Posts
    10,943

    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.

  4. #4
    Join Date
    Mar 2003
    Posts
    95

    Re: What Sub/Function/Block of Code Produced Error

    Quote Originally Posted by dlarkin77 View Post
    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

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: What Sub/Function/Block of Code Produced Error

    Try this :

    Code:
    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!
    Attached Files Attached Files
    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
  •  





Click Here to Expand Forum to Full Width

Featured