## Intro

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

• Breakpoints stop a program while it is running.
• Immediate window is used to immediately run, within the current scope, the statements entered.
• If you enter the statement Debug.Print variable in your code, the value of the variable will show in the immediate window.
• Watch expressions monitor particular variables or expressions. The values are updated at break points.
• Step options run portions of code either one statement or one procedure at a time.
• Local window displays all the declared variables in the current value, including their values.
• Call Stack views all active procedure calls and traces the execution of nested procedures.

## Common Debugging Shortcuts

• CTRL-BREAK Enters break mode, i.e. temporarily suspends the execution of the program during development.
• F5 Executes the program or resume execution.
• F8 Single Step Into. Executes the current line, advances to the next line, and breaks (single step into).
• SHIFT-F8 Procedure Step Over. Executes the current line and if, it contains a call to a procedure, executes that, and then advances to the next line, and breaks.
• CTRL-SHIFT-F8 Procedure Step Out. Just like Step Over except that it advances past the remainder of the code in the current procedure. If the procedure was called from another procedure, it advances to the statement immediately following the one that called the procedure.

## The Err Object

• The Err object stores information about run-time errors.
• An error generator may be Visual Basic, an object, or a program statement. The generator sets the properties of the Err object.
• The Err object is an intrinsic object of global scope: it does not have to be declared.
• Utilize the properties and methods of the Err object to check which error occurred, to clear the error value, or to raise an error.
• Description property.
• HelpContext property returns or sets the Context ID for a topic in a Help file.
• HelpFile property returns or sets the fully qualified path to a Help file.
• LastDLLError property returns a system error code returned by a call to a DLL.
• Number property is the default property. It is an integer indicating which error occurred.
• Source property contains the name of the object that generated the error.
• Clear() method explicitly resets the value of Err.Number back to zero or empty string.
• Raise() method generates an error. Here is the Raise method syntax:
Err.Raise number_0_to_65535,
source_ie_curr_VB_project,
description_string,
helpfile,
helpcontext
• The properties of the Err object are reset to zero or empty strings:
• After an Exit Sub, Exit Function, Exit Property
• After any variation of Resume statement within an error-handling routine
• Not after any variation of Resume statement prior to the error-handling routines

## 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:
ErrorHandler:
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
ErrorHandler:
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.
• Pull down menu Tools, Options, General and then choose. Choosing here affects subsequent sessions in VB.
• Right click on Code window, Toggle, and then choose. Choosing here affect only your current session.
Here are the distinctions between your error trapping options.
• Break on All Errors. Breaks wherever or whenever an error occurs.
• Whether it occurs in a Class module or not.
• Whether the error has error handling or not.
• Break on Unhandled Errors.
• If the error occurs in a Class module, then it breaks at the calling procedure if the calling procedure has no error handling.
• If the error occurs outside of a Class module, then it breaks at the procedure if the procedure has no error handling.
• Break in Class Modules.

## 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