Visual Basic provides various interactive debugging tools for finding run-time errors and program logic errors through the Debug menu or the Debug toolbar.

Common Debugging Shortcuts

The Err Object

Handling Run-time Errors

Here is how to set an error trap.
  1. Place On Error Go To <label> prior to potential errors. This will send control to the label of an error handling routine in the same procedure. (Use On Error GoTo 0 to disable error handling.) Note that VBS does not support labels.
  2. If desired, an error can be raised with Err.Raise prior to the error handling routines if you are expecting a particular kind of error. A user-defined error is usually assigned a Err.Number of vbObjectError + n.
  3. At the end of the procedure but prior to your error handlers, place an Exit Sub, Exit Function, or Exit Property statement. If this is not in place, then the error handling code will be executed even if there was no error.
  4. Label your error handler. The format is the name followed by a colon. EG:
  5. Write the error handling routine. Place code utilizing the Err object here.
  6. Exit the error-handling code.
    • Resume exits to repeat the operation that cause the error.
    • Resume Next exits to execute the statement immediately after the operation that cause the error.
    • Resume label exits to the specified label or line.

Alternatively use On Error Resume Next to resume the program at the line following the error. This basically ignores errors.

Error-handling Example

Private Sub SubName
On Error GoTo ErrorHandler    'Starts error handling
On Error GoTo 0               'Turns off error handling
On Error GoTo ErrorHandler    'Restarts error handling
ResumeHere:                   'The error handler can resume the app here
On Error Resume Next          'Handles errors by ignoring them
Err.Raise vbObjectError + 57, , "User-defined error"
   Exit Sub                      'Required
   Dim sErrorMessage As String
   If Err.Number = (vbObjectError + 57) Then MsgBox "My error #57"
   sErrorMessage = _
      "Number: " & Str(Err.Number) & ". " & _
      "Description: " & Err.Description & ". " & _
      "Source: " & Err.Source & "."
   MsgBox sError, , "Error", Err.HelpFile, Err.HelpContext
   Debug.Print sError
   Resume Next
   'These variations of Resume could also have been used: 
   ' Resume [back to the error producing line] 
   ' Resume Next [goes to the line following the error producing line] 
   ' ResumeHere [sends control to a label]
End Sub

Error Trapping Options

There are two ways to set your error trapping options. Here are the distinctions between your error trapping options.

Common Error Numbers

Code Message Code Message
5 Invalid procedure call 57 Device I/O error
6 Overflow 58 File already exists
7 Out of memory 59 Bad record length
9 Subscript out of range 61 Disk full
10 This array is fixed or temporarily locked 62 Input past end of file
11 Division by zero 63 Bad record number
13 Type mismatch 67 Too many files
14 Out of string space 71 Disk not ready
16 Expression too complex 74 Can't rename with different drive
20 Resume without error 75 Path/File access error
35 Sub, Function, or Property not defined 76 Path not found
52 Bad filename or number 92 For loop not initialized
53 File not found 93 Invalid pattern string
54 Bad file mode 31036 Error saving to file
55 File already open 31037 Error loading from file

Page Modified: (Hand noted: ) (Auto noted: )