Error Handler

To do effective error handling, you must write them into each and every routine. What a pain! There are many ways to do error handling, from coding it yourself to purchasing add-on tools that will do most of the code writing for you.

This tip is for those who prefer to do it themselves. This is a good idea, because you have much more control over it. Also, another important part of error handling is writing the error to a file. This will save you tons of time in development and also help with technical support problems. Instead of trying to get information on the error message from the user, they can simply send the error log. This tip incorporates that philosophy.

Declare

Global Const errExit = 0
Global Const errResume = 1
Global Const errNext = 2
Global Const errSelect = 3

Code

Public Function ErrorHandler(iErrorNumber As Integer, sErrText As String, 
iErrOption As Integer) As Integer
Dim sMessage As String
Dim iReturn As Integer
     'Create message string
     sMessage = "Error #:" & Str(iErrorNumber) & " - "
     sMessage = sMessage & sErrText
     'Save to error log file
     ErrWriteLogFile sMessage
 
     Select Case iErrOption
           Case errExit
                 MsgBox sMessage, vbCritical, _
                       "Exiting program..."
                 GoTo errHandlerEnd
           Case errResume
                 MsgBox sMessage, vbCritical, "Error"
                 ErrorHandler = errResume
           Case errNext
                 MsgBox sMessage, vbCritical, "Error"
                 ErrorHandler = errNext
           Case errSelect
                 iReturn = MsgBox(sMessage, vbCritical + _
                       vbAbortRetryIgnore, "Error")
                 Select Case iReturn
                       Case Is = vbAbort
                             GoTo errHandlerEnd
                       Case Is = vbRetry
                             ErrorHandler = errResume
                       Case Is = vbIgnore
                             ErrorHandler = errNext
                 End Select
     End Select
     Exit Function
errHandlerEnd:
     MsgBox "Click OK to Exit Program"
     End
End Function
 
Public Sub ErrWriteLogFile(sLogMsg As String)
     Dim sFile As String
     Dim lFile As Long
     Dim sErrDir As String
 
     On Error GoTo errWriteLogFileErr
     sErrDir = App.Path
     lFile = FreeFile
     sFile = sErrDir & "\" & App.EXEName & ".err"
     Open sFile For Append As lFile
     Print #lFile, Format$(Now, "General Date") & ": " _
           & sLogMsg
     Close #lFile
     GoTo errWriteLogFileExit
errWriteLogFileErr:
     MsgBox Str(Err) + "-" + Error$, vbCritical, _
           "Unable to Write Error Log"
     Exit Sub
errWriteLogFileExit:
End Sub

Example

This is a sample of how you could use this tip:

Dim R As Long
Dim I As Integer
On Error GoTo ErrorHandler1
     For I = 3000 To 100000
           DoEvents
     Next I
ErrorHandler1:
     R = ErrorHandler(iErrorNumber:=Err.Number, sErrText:=Err.Description, iErrOption:=errNext)

Even though this tip was written in Visual Basic 4.0, it can be easily modified for any other version of Visual Basic. It’s also easy to modify for your particular needs.

 

This tips is reprinted from the VB Tips & Tricks Volume 1 book.
Some parts of this tips was submitted by: George Graff

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s