Lesson 35: Errors Handling in VB6
Implement robust error handling to create reliable applications
Key Takeaway
Robust error handling is essential for creating professional, reliable VB6 applications that can gracefully handle unexpected situations.
Welcome to Lesson 35 of our Visual Basic 6 Tutorial! In this lesson, you'll learn how to implement robust error handling in your VB6 applications. Proper error handling prevents crashes, improves user experience, and makes your applications more reliable.
35.1 Understanding Errors
In Visual Basic programming, effective error handling is a crucial practice to ensure the seamless operation of a program. By addressing errors, programmers can significantly enhance the program's reliability and performance. A smoothly running, error-free program minimizes the likelihood of issues like program crashes or system hang-ups, offering users a hassle-free experience.
Common errors often stem from inaccurate user inputs. For instance, attempting to instruct the computer to divide a number by zero is a classic mistake that leads to a system error. Similarly, inputting text (string) into a field designed exclusively for numeric values, such as a person's weight, can disrupt the program's ability to perform arithmetic calculations, resulting in an error. These types of errors, referred to as synchronous errors, highlight the importance of robust error-handling mechanisms in Visual Basic programming.
User Input Errors
Invalid data types, out-of-range values, or unexpected formats
Resource Access
Missing files, locked databases, or unavailable network resources
Calculation Errors
Division by zero, overflow conditions, or invalid math operations
Best Practice
A good programmer should be alert to parts of the program that could trigger errors and should write error handling code to help manage them. However, avoid excessive error handling as it can complicate maintenance.
35.2 Error Handling Syntax
The core syntax for error handling in VB6 is:
On Error GoTo program_label
Where program_label
is the section of code designed to handle errors. When an error occurs, the program jumps to this label.
Example 35.1: Division by Zero
This example demonstrates handling division by zero errors:
Private Sub CmdCalculate_Click() Dim firstNum, secondNum As Double firstNum = Txt_FirstNumber.Text secondNum = Txt_SecondNumber.Text ' Enable error handling On Error GoTo error_handler Lbl_Answer.Caption = firstNum / secondNum ' Prevent executing error handler when no error Exit Sub ' Error handling section error_handler: Lbl_Answer.Caption = "Error" Lbl_ErrorMsg.Visible = True Lbl_ErrorMsg.Caption = "You attempted to divide by zero! Try again!" End Sub Private Sub Txt_FirstNumber_GotFocus() Lbl_ErrorMsg.Visible = False End Sub Private Sub Txt_SecondNumber_GotFocus() Lbl_ErrorMsg.Visible = False End Sub
Explanation
The Exit Sub
statement is crucial to prevent the error handler from executing when there are no errors. The GotFocus events clear any previous error messages when the user interacts with the textboxes again.

35.3 Nested Error Handling
For more complex scenarios, you can implement nested error handling to manage multiple potential errors:
Private Sub CmdCalculate_Click() Dim firstNum, secondNum As Double ' First error handler: non-numeric inputs On Error GoTo error_handler1 firstNum = Txt_FirstNumber.Text secondNum = Txt_SecondNumber.Text ' Second error handler: division by zero On Error GoTo error_handler2 Lbl_Answer.Caption = firstNum / secondNum Exit Sub ' Division by zero handler error_handler2: Lbl_Answer.Caption = "Error" Lbl_ErrorMsg.Visible = True Lbl_ErrorMsg.Caption = "Division by zero error! Try again!" Exit Sub ' Prevent falling through to error_handler1 ' Non-numeric input handler error_handler1: Lbl_Answer.Caption = "Error" Lbl_ErrorMsg.Visible = True Lbl_ErrorMsg.Caption = "Non-numeric input! Try again!" End Sub
Important
Each error handler needs its own Exit Sub
to prevent executing subsequent error handlers. The order of handlers matters - put more specific handlers before more general ones.


Lesson Summary
In this lesson, you've learned essential error handling techniques in VB6:
Error Handling Syntax
Master the On Error GoTo statement to handle runtime errors
Error Prevention
Identify common error sources like invalid user inputs
Nested Handling
Implement multiple error handlers for complex scenarios
User Experience
Provide clear error messages to guide users
Pro Tip
Combine error handling with input validation to prevent errors before they occur. Use IsNumeric() for numeric fields and proper data type checking.
Next Lesson
Continue your VB6 journey with Lesson 36: Compiling and Distributing Applications.
Related Resources

Visual Basic 6 Made Easy
The comprehensive guide to mastering VB6 development, including professional error handling techniques.
What You'll Learn:
- Error handling fundamentals
- On Error GoTo implementation
- Nested error handling
- User-friendly error messages
- Debugging techniques

Visual Basic 2022 Made Easy
The modern guide to VB.NET programming with Visual Studio 2022. Master modern error handling techniques.
What You'll Learn:
- Structured exception handling
- Try-Catch-Finally blocks
- Custom exception classes
- Global error handling
- Logging and diagnostics