Geodatabase Designer
bin\modErrorHandlerNew.bas

' Copyright 2008 ESRI
'
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
'
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
'
' See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.


Option Explicit

'--------------------------
' Error Log File Extension
'--------------------------
Private Const ERRORFILEEXTENSION As String = ".err"

Public Sub HandleError(ByVal pTopProcedure As Boolean, _
                       ByVal pProcedureName As String, _
                       ByVal pErrNumber As Long, _
                       ByVal pErrSource As String, _
                       ByVal pErrDescription As String)
    '---------------------------------------------------------------------------------------
    ' Generic Error handling Function - This function should be called with
    ' the following Arguments
    ' Boolean    -in-  True if called from a top level procedure - Event / Method / Property
    ' String     -in-  Name of function called from
    ' Long       -in-  Error Number (retrieved from Err object)
    ' String     -in-  Error Source (retrieved from Err object)
    ' String     -in-  Error Description (retrieved from Err object)
    '---------------------------------------------------------------------------------------
    Dim pWriteLine As String
    Dim pTimeStamp As String
    Dim pFileName As String
    Dim pFSO As Scripting.FileSystemObject
    Set pFSO = New Scripting.FileSystemObject
    '------------------------
    ' Clear the error object
    '------------------------
    Err.Clear
    '-----------------------------------------------------------
    ' Static variable used to control the call stack formatting
    '-----------------------------------------------------------
    Static pEntered As Boolean
    If pTopProcedure Then
        '----------------------------------------------------------
        ' Top most procedure in call stack so report error to user
        '----------------------------------------------------------
        If Not pEntered Then
           pErrDescription = vbTab & "Error Number:      " & CStr(pErrNumber) & vbCrLf & _
                             vbTab & "Error Description: " & pErrDescription
        End If
        pEntered = False
        '-----------------------------------------
        ' Prefix Error Message with Date and Time
        '-----------------------------------------
        pTimeStamp = "[DATETIME: " & CStr(Year(Date)) & ":" & _
                                     CStr(Month(Date)) & ":" & _
                                     CStr(Day(Date)) & "_" & _
                                     CStr(Hour(Time)) & ":" & _
                                     CStr(Minute(Time)) & ":" & _
                                     CStr(Second(Time)) & _
                     " DLLVERSION: " & CStr(App.Major) & "." & _
                                       CStr(App.Minor) & "." & _
                                       CStr(App.Revision) & _
                     "] "
        '---------------------------
        ' Construct Error File Name
        '---------------------------
        pFileName = pFSO.BuildPath(App.Path, App.EXEName) & ERRORFILEEXTENSION
        '---------------------------------
        ' Construct Line to Write to File.
        '---------------------------------
        pWriteLine = pTimeStamp & vbCrLf & _
                     vbTab & "Procedure:         " & pProcedureName & vbCrLf & _
                     pErrDescription
        Call WriteFile(pFileName, pWriteLine)
        '--------------------------------------------------------
        ' Make a noise the display an Error Message to the user.
        '--------------------------------------------------------
        MsgBox "An application error has occurred. Please check the Error Log:" & vbCrLf & _
               pFileName, _
               vbCritical + vbOKOnly, _
               App.FileDescription
    Else
        '------------------------------------------------------------------
        ' An error has occured but we are not at the top of the call stack
        ' so append the callstack and raise another error.
        '------------------------------------------------------------------
        If Not pEntered Then
            pErrDescription = vbTab & "Error Number:      " & CStr(pErrNumber) & vbCrLf & _
                              vbTab & "Error Description: " & pErrDescription
        End If
        pEntered = True
        Err.Raise pErrNumber, pErrSource, vbTab & Space(19) & pProcedureName & vbCrLf & pErrDescription
    End If
End Sub