Visual Basic Testing Debugging Tools
Visual Basic Testing Debugging Tools
1. Error Handling
Error handling prevents the program from crashing due to unexpected issues like dividing
by zero, file not found, etc.
Techniques:
- On Error GoTo [Label] – Directs code to an error-handling block.
- On Error Resume Next – Skips the error and continues with the next line.
- Err Object – Contains error number (Err.Number) and description (Err.Description).
Example:
On Error GoTo errHandler
Dim x As Integer
x = 5 / 0 ' Causes divide by zero error
Exit Sub
errHandler:
MsgBox "Error: " & Err.Description
2. Debugging Tools
These help you detect and fix logical or runtime errors.
Key Tools:
- Breakpoints (F9)
- Step Into (F8)
- Step Over (Shift + F8)
- Watch Window
- Call Stack
Common Uses:
- Print variable values using '? variableName'
- Run lines of code instantly
Shortcut: Ctrl + G
Example:
? Text1.Text ' Prints the value of Text1 textbox
4. Testing Programs
Running your code with different inputs to find bugs.
Types:
- Unit Testing
- Boundary Testing
- Negative Testing
- Functional Testing
Methods:
- Stop Statement
- Breakpoints
Example:
Stop
MsgBox "Paused for Debugging"
Example:
If Dir("C:\data.txt") <> "" Then
Open "C:\data.txt" For Input As #1
' Read data
Close #1
Else
MsgBox "File not found"
End If
Conclusion
Visual Basic provides tools to detect, prevent, and correct errors. By combining structured
error handling, debugging tools, and proper testing strategies, developers can create robust
and reliable applications.