Debugging And Error Handling

Tips >> Visual Basic


Visual Basic has built in Error Scanning to check for syntax errors. For example, if you forget to put the "Then" keyword at the end of an If statement, you get an error message saying, "Expected: Then or GoTo".

If day = 2


There are three types of error that can occur:
1. Compile-Time.
2. Run-Time
3. Logical.

Visual Basic's error scanning only detects errors made on a single line. A missing "End If" is an example of a compile-time error, and would be detected when the program is compiled before a run. Errors can also arise during the execution of a program and are called run-time errors.
An example of a run-time error might be the wrong data type provided for a field in a database or trying to divide by zero.
Logical errors are the hardest to detect as they follow the rules of the programming language, but behave in a manner that was not intended. Logical errors occur through incorrect logic such as using a logical "And" instead of a logical "Or" in a condition.


Make sure the Debug toolbar is visible by putting a check against "Debug" in the "View", "Toolbars" menu item. When the program is running, you can pause it so you can examine values or set test value by pressing the "Break" button on the toolbar.

The toolbar buttons are described in the rest of this tutorial.


You can examine variables directly in the Intermediate Pane with the Print command, and set values with the assignment operator. Typing, Print Me.Left would return the left position of the form, and typing me.left = 1000 would set the left position of the form to 1000.

You can also print to the Intermediate Pane from Visual basic with the Debug object's Print method.

Debug.Print Me.ScaleWidth - shpBall.Left + shpBall.Width


The Debug object's Assert method may be used to enter "Break Mode" should condition not be true.
The following will go into Break Mode if a Shape is positioned outside the right hand side of the Form.

Debug.Assert shpBall.Left + shpBall.Width & Me.ScaleWidth


Breakpoints and Watches are two useful features of the debugger, that allow you to halt the program at a certain point or observe variables to track the logic.


The Toggle Breakpoint command is used to add/remove breakpoints where the cursor is positioned in the code window. Breakpoints cause the program to enter "Break Mode", and display the block of code on screen in order you may examine values and step through the code. Breakpoints may also be added by clicking on the "Margin Indicator Bar" on the left of the code window.


The Debug Step commands allow you to step through the program in order to visually trace the flow of execution. The Step Into command executes the current statement. If the current statement is a procedure or function, the next statement will be the first line of the procedure or function. The Step Over command executes the current statement. If the current statement is a procedure or function, the procedure or function is executed as a single statement. The Step Out command executes the remaining statements in a procedure or function.


The Locals Window displays the value and data types of all variables in the module or form. You can also inspect the state of the Form's properties with the "Me" entry.


Variables are add to the watch window by either right-clicking the variable and selecting "Add Watch" or by selecting "Add Watch" from the "Debug" menu.

"Break When Value Is True" causes the program to enter "Break Mode", should the value be True, and Break When Value Changes causes the program to enter "Break Mode" whenever the value changes.


Variables may be added to the watch window by highlighting the variable and selecting "Quick Watch" from the "Debug" menu.

Clicking "Add", adds the variable to the Watch window.


The Call Stack window allows you to see the list of procedures that were called in order to get to the current statement. The window may either be displayed by selecting "Call Stack" from the "View" menu or by clicking the "Locals" window's "Call Stack" button.


The On Error statement is used to trap errors in Visual Basic 6. VB.Net uses the Try, Catch methods in order to eliminate the need for a GoTo statement. The On Error command must be placed in the procedure at a position before the error could arise. The "Exit Sub" statement is used before the error handling part in order to avoid running the error handling code when there is no error. The Error object is built-in with global scope and contains the state of errors that may have occurred. The Number property provides the error number, and the Description property is a translation of the error.

Private Sub drvList_Change()
     On Error GoTo driveError
     dirList.Path = drvList.Drive
     Exit Sub
     MsgBox Err.Description, vbExclamation, "Drive Error"
End Sub



The Resume statement is used to specify where to restart the flow of execution. This can either be a label, or Next. If Next is used, execution is continued from the statement following the statement that caused the error.
The following example prompts the user what to do should a drive error occur using the, Resume Next statement.

Private Sub drvList_Change()
     On Error GoTo driveError
     dirList.Path = drvList.Drive
     Exit Sub
     Dim response As Integer, description As Integer
     description = vbExclamation + vbRetryCancel
     response = MsgBox(Err.Description, description, "Drive Error")
     If response = vbRetry Then
          Resume retryDrive
     End If
End Sub


On Error Resume Next, skips any run-time errors that may occur. On Error GoTo 0, turns error trapping off.
The following example ignores errors.

Private Sub drvList_Change()
     On Error Resume Next
     dirList.Path = drvList.Drive
End Sub



The Raise method of the Err object may be used to rethrow the error. This is particularly useful with class modules, as you can specify the source and a description of the problem.

description = "Unable to process the data provided"
Err.Raise Err.Number, "myObject", description


The following example handles all errors returned from the InputBox, except incorrect data types (Error code 13).

Dim age As Integer
On Error GoTo incorrectDataType
age = InputBox("Enter your name", "Age")
Me.Print "You are " & age & " years old"
Exit Sub
If Err.Number = 13 Then
     Err.Raise 13
     MsgBox Err.description, vbExclamation, "Error"
End If


If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.


News / Updates : New Money Order MLM Plan! First time in INDIA to implement Money Order MLM. New version of Cheque Printing Software is up.

E-mail :
Phone : +91 022 28067978