Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining what event is fired

Status
Not open for further replies.

UncleT

Programmer
Sep 26, 2001
133
0
0
US
Hello, I've got an error routine that I usually put into each event I create. This routine has global variables in which I put the name of the event I am currently in. For example g_str_Error_Procedure = "cmdbutton_Click". Right now I am copying and pasting the "cmdbutton_click" into the code. My question is, in VB2005 is there a variable already set inside each procedure that contains this information so I can just create a generic error routine and just paste it?

Thanks,

UncleT
 
You need a generic error/exception handler. The Try/Catch/Finally block will help you along. Take a look at the link for some basic exception handling


Sweep
...if it works, you know the rest..
Always remember that Google is your friend

curse.gif
 
The Try/Catch method is basicly what I use in my error routine. What I am asking is, is there a system variable that describes what Procedure you are currently in? Example a command event like "cmdbutton_click" or a procedure written by the programmer "m_s_Load_Grid".

Thanks,

UncleT
 
If you are using try catch, then take a look at what properties are available on the exception (ex)

ex.Source, ex.Stacktrace and ex.Message should give you all you need.

Sweep
...if it works, you know the rest..
Always remember that Google is your friend

curse.gif
 

Here's a class I wrote for doing this:


Code:
Public Class ErrParser
    Public Shared Function GetProcName(ByVal ErrStackTrace As String) As String
        Dim ExeName As String
        Dim LastSlashLoc As Integer
        Dim NameLoc As Long
        Dim TempStr As String
        Dim ParenLoc As Integer

        ExeName = System.Reflection.Assembly.GetExecutingAssembly.GetName.Name

        ExeName = Replace(ExeName, " ", "_")

        NameLoc = ErrStackTrace.IndexOf(ExeName)
        TempStr = Mid(ErrStackTrace, NameLoc + Len(ExeName) + 2)
        ParenLoc = TempStr.IndexOf("(")
        GetProcName = Mid(TempStr, 1, ParenLoc)
    End Function

    Public Shared Function GetErrLineNum(ByVal ErrStackTrace As String) As String
        Dim LineLoc As Long

        LineLoc = ErrStackTrace.LastIndexOf(":line")
        GetErrLineNum = Mid(ErrStackTrace, LineLoc + 6)
    End Function

    Public Shared Sub LogError(ByVal ErrMsg As String)
        Dim sw As System.IO.StreamWriter

        Try
            sw = New System.IO.StreamWriter(Application.StartupPath & "\DMS_Error_Log.log", True)
            sw.WriteLine("****************************************************************************")
            sw.WriteLine(Date.Now.ToString)
            sw.WriteLine(ErrMsg)
            sw.Close()
        Catch ex As Exception

        End Try

    End Sub
End Class


'Usage Template

'Dim ep As New ErrParser

'Try
'   Some Code Here
'Catch ex As Exception
'   Dim ErrMsg As String
'   ErrMsg = "Error in " & ep.GetProcName(ex.StackTrace)

'#If DEBUG Then
'   ErrMsg &= " in Line " & ep.GetErrLineNum(ex.StackTrace)
'#End If

'   ErrMsg &= vbCrLf
'   ErrMsg = ErrMsg & "Error Message: " & ex.Message
'   MsgBox(ErrMsg, MsgBoxStyle.Exclamation Or MsgBoxStyle.OKOnly, "Message Box Title Here")
'End Try


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top