Results 1 to 5 of 5

Thread: Global error handler with stack tracing

Threaded View

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Arrow Global error handler with stack tracing

    This is just a sample of how to use a global error handler that will log application errors together with a stack tracing implementation.

    These are the main functions that does the logging.
    VB6 Code:
    1. Public Function PromptAndLogError(ByVal strErrorSource As String, Optional ByVal strOtherInfo As String = vbNullString, Optional ByVal ReplyButton As Long = 2) As Long
    2.     Dim strErrorMessage As String
    3.     Dim strMsg          As String
    4.     Dim strStack        As String
    5.     Dim strErrDesc      As String
    6.     Dim strErrNum       As String
    7.     Dim strErl          As String
    8.     Dim dteErrorOccured As Date
    9.        
    10.     'store the pertinent error values
    11.     strErrDesc = Err.Description
    12.     strErrNum = Err.Number
    13.     strErl = Erl
    14.    
    15.     dteErrorOccured = Now 'get date
    16.     strStack = GetTraces  'get stack trace
    17.    
    18.     strErrorSource = ProgramName & "." & strErrorSource
    19.    
    20.     If Len(strStack) > 0 Then
    21.         strErrorMessage = "Last Trace:        " & strStack & vbCrLf
    22.     End If
    23.     'this will be the logged message, it will contain the stack trace
    24.     strErrorMessage = strErrorMessage & _
    25.                     "Error Occured In:  " & strErrorSource & vbCrLf & _
    26.                     "Error Date & Time: " & dteErrorOccured & vbCrLf & _
    27.                     "Error Description: " & strErrDesc & vbCrLf & _
    28.                     "Error Number:      " & strErrNum & vbCrLf & _
    29.                     "Line Number:       " & strErl
    30.  
    31.     'save other pertinent data that you want to be logged
    32.     If Len(strOtherInfo) > 0 Then
    33.         strErrorMessage = strErrorMessage & (vbCrLf & strOtherInfo)
    34.     End If
    35.    
    36.     'this will be the message to be show to the user
    37.     strMsg = "Error Occured In: " & strErrorSource & vbCrLf & _
    38.              "Error Date & Time: " & dteErrorOccured & vbCrLf & _
    39.              "Error Description: " & strErrDesc & vbCrLf & _
    40.              "Error Number: " & strErrNum & vbCrLf & _
    41.              "Line Number: " & strErl
    42.    
    43.     PromptAndLogError = LogAndProcessError(strMsg, strErrorMessage, dteErrorOccured, True, ReplyButton)
    44. End Function
    45.  
    46. Private Function LogAndProcessError(ByVal MsgBoxError As String, ByVal ErrorMessage As String, ByVal dteErrorOccured As Date, Optional ShowPrompt As Boolean = True, Optional ByVal ReplyButton As Long = 2) As Long
    47.         On Error GoTo HandleError
    48.        
    49.         Dim FileNum         As Integer
    50.         Dim strErrorMessage As String
    51.         Dim Response        As String
    52.         Dim ErrorFile       As String
    53.         Dim PCName          As String
    54.         Dim ScreenFile      As String
    55.         Dim strData         As String
    56.         Dim ProgramPath     As String
    57.        
    58. 100     SkipTrace = True
    59. 102     ProgramPath = strAppData
    60.        
    61.         'also log error in event viewer
    62. 104     App.StartLogging "", vbLogToNT
    63. 106     App.LogEvent ErrorMessage, vbLogEventTypeError
    64.    
    65.         'get computer name
    66. 108     PCName = GetPCName
    67.  
    68.         'create directory where the error will be logged if the directory does not yet exist
    69. 110     If DirectoryExists(ProgramPath & "\ErrorLog\") = False Then
    70. 112         CreateFolder ProgramPath & "\ErrorLog\"
    71.         End If
    72.        
    73.         'this will create a unique file for each day
    74. 114     ErrorFile = ProgramPath & "\ErrorLog\ErrorLog-" & PCName & "-" & Format$(dteErrorOccured, "mmddyyyy") & ".txt"
    75. 116     ScreenFile = ProgramPath & "\ErrorLog\ErrorLog-" & PCName & "-" & Format$(dteErrorOccured, "mmddyyyy-hhnnss") & ".jpg"
    76.        
    77. 118     If ShowPrompt = True Then
    78. 120         LogAndProcessError = MsgBox(MsgBoxError & vbCrLf & vbCrLf & "Please report to the programmer any persistent error.   ", vbCritical + ReplyButton + vbDefaultButton2, "Unexpected Error Encountered")
    79.             'log also the response of the user
    80. 122         Select Case LogAndProcessError
    81.             Case vbAbort
    82. 124             Response = "vbAbort"
    83. 126         Case vbRetry
    84. 128             Response = "vbRetry"
    85. 130         Case vbIgnore
    86. 132             Response = "vbIgnore"
    87. 134         Case vbOK
    88. 136             Response = "vbOK"
    89.             End Select
    90. 138         If Len(Response) > 0 Then
    91. 140             ErrorMessage = ErrorMessage & vbNewLine & "Response:          " & Response
    92.             End If
    93.         End If
    94.    
    95.         'if the log file already exist then append the new error log
    96. 142     If FileExists(ErrorFile) = True Then
    97. 144         strData = vbCrLf & String$(20, "-") & vbCrLf & ErrorMessage
    98.         Else
    99. 146         strData = ErrorMessage
    100.         End If
    101.    
    102. 148     FileNum = FreeFile
    103. 150     Open ErrorFile For Append As FileNum
    104. 152     Print #FileNum, strData
    105. 154     Close FileNum
    106.    
    107.         'capture screenshot
    108. 156     Capture_Desktop ScreenFile
    109.        
    110. 158     Busy False
    111. 160     SkipTrace = False
    112.  
    113.         Exit Function
    114. HandleError:
    115.     strErrorMessage = vbNewLine & _
    116.              "Error Occured In:  " & "LogAndProcessError" & vbCrLf & _
    117.              "Error Date & Time: " & dteErrorOccured & vbCrLf & _
    118.              "Error Description: " & Err.Description & vbCrLf & _
    119.              "Error Number:      " & Err.Number & vbCrLf & _
    120.              "Line Number:       " & Erl
    121.     LogMessage strErrorMessage
    122.     Busy False
    123. End Function

    And for the stack tracing:
    VB6 Code:
    1. Public Sub PushStack(ByVal strProcedure As String)
    2.     'when in error handling routine then don't register traces
    3.     If SkipTrace = True Then Exit Sub
    4.  
    5.     If ArrayInit(Not colStacks) = False Then
    6.         'initialized
    7.         ReDim Preserve colStacks(0)
    8.         colStacks(0) = strProcedure
    9.     Else
    10.         'increase size
    11.         ReDim Preserve colStacks(UBound(colStacks) + 1)
    12.         colStacks(UBound(colStacks)) = strProcedure
    13.     End If
    14. End Sub
    15.  
    16. Public Sub PopStack()
    17.     If SkipTrace = True Then Exit Sub
    18.  
    19.     If ArrayInit(Not colStacks) = True Then
    20.         If UBound(colStacks) = LBound(colStacks) Then
    21.             Erase colStacks
    22.         Else
    23.             ReDim Preserve colStacks(UBound(colStacks) - 1)
    24.         End If
    25.     End If
    26. End Sub

    Sample usage:
    VB6 Code:
    1. Private Sub Command1_Click()
    2.         PushStack "Form1.Command1_Click"
    3.         On Error GoTo HandleError
    4.    
    5.         Dim a As Long
    6.         Dim b As Long
    7.         Dim c As Long
    8.        
    9. 100     a = 0
    10. 102     b = 1
    11. 104     c = b / a 'division by zero
    12.    
    13. ExitProcedure:
    14.         PopStack
    15.         Exit Sub
    16. HandleError:
    17.         Select Case PromptAndLogError("Form1.Command1_Click")
    18.         Case vbAbort
    19.              GoTo ExitProcedure
    20.         Case vbRetry
    21.              Resume
    22.         Case vbIgnore
    23.              Resume Next
    24.         Case Else
    25.              SignalCriticalError Err.Description
    26.              End
    27.         End Select
    28. End Sub

    If you happen to use MZTools then this will be the template that you can use:
    Code:
    PushStack "{MODULE_NAME}.{PROCEDURE_NAME}"
       On Error GoTo HandleError
    
    	{PROCEDURE_BODY}
    
    ExitProcedure:
       PopStack
       Exit {PROCEDURE_TYPE}
    
    HandleError:
    
    	Select Case PromptAndLogError("{MODULE_NAME}.{PROCEDURE_NAME}")
            Case vbAbort
                 GoTo ExitProcedure
            Case vbRetry
                 Resume
            Case vbIgnore
                 Resume Next
            Case Else
                 SignalCriticalError Err.Description
                 End
            End Select
    Have a look at the attached project for a complete demo.
    Attached Files Attached Files
    Last edited by dee-u; Aug 31st, 2009 at 03:47 AM. Reason: added MZTools template
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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