Dealing with errors in Visual Basic 6

 

Click here to download the sample project for this tutorial

There are often occasions when the code you are writing has a high potential for going wrong. An example might be opening a file - if the file isn't there a "File not found" error will occur.

 

The standard error messages in VB6 aren't really very helpful for users of your program that don't really understand computers very well so it would be helpful to them if you could create error messages that tells them what really went wrong.

Example code - a division by zero

In my example code below I have created two variables - iTotal and iEvilZero. As you are probably aware, you cannot divide by zero - it's a mathematical impossibility. If your program tries to divide by zero it will end with a nasty error message.

Private Sub Form_Load()
    ' The Form_Load event is triggered when the program starts and
    ' this form is loaded before showing it to the user.


    ' Declare a variable called iNumber to store whole numbers (integers)
    Dim iNumber As Integer
   
    ' Declare a variable called iEvilZero to store whole numbers (integers)
    Dim iEvilZero As Integer
   
    'Store 432 in iNumber
    iNumber = 432
   
    ' Store zero in iEvilZero
    iEvilZero = 0
   
    ' Divide iNumber by iEvilZero and store the result in iNumber
    iNumber = iNumber / iEvilZero
End Sub

Adding the error handler

Because you know that any division by a variable is dangerous (the variable could contain zero) it would be a good idea to make sure that you trap any errors that occur during the division.

To do this, just before the line that performs the division, enter the code below in the red dotted box:

' Store zero in iEvilZero
iEvilZero = 0
    
' When an error occurs, jump to the divideError label.
On Error GoTo divideError   

' Divide iNumber by iEvilZero and store the result in iNumber
iNumber = iNumber / iEvilZero

What this line of code does is tell VB6 that if an error occurs, to jump to a label called divideError and continue from there.

Labels are special markers in your programming code that can be jumped to. You will need to create the label to jump to.

Creating the label to jump to

Insert some empty lines just before the End Sub line and add the code in the red dotted box below. Don't add another End Sub - that's just there to show you where to add the code.

   ' Divide iNumber by iEvilZero and store the result in iNumber
    iNumber = iNumber / iEvilZero

   ' Prevent the error code being executed when there wasn't an error
    Exit Sub

divideError: 

   ' Code here should only be executed if an error happens
 

End Sub

The Exit Sub is there so that the sub-routine is exited nicely if nothing goes wrong in the code above. If it wasn't there, even if there wasn't an error, the code below the divideError label would still be executed.

Displaying a message when something goes wrong

Finally, you need to add some code to display a polite error message if an error does occur (which in my example code is 100% guaranteed).

Just before the End Sub, add the following lines of code in the dotted red box to display a message box:

divideError:

' Display a polite message box with a critical icon
MsgBox "Sorry, I can't divide by zero.", vbCritical, "Error"

' Close the program (this error could be critical)
End

End Sub

 

Putting it all together

When your run the program it should display your polite message box because the program tries to divide by zero.

Here's what the completed code should look like (just in case you're not sure you did it right):

Option Explicit

Private Sub Form_Load()
    ' The Form_Load event is triggered when the program starts and
    ' this form is loaded before showing it to the user.

    ' Declare a variable called iNumber to store whole numbers (integers)
    Dim iNumber As Integer
   
    ' Declare a variable called iEvilZero to store whole numbers (integers)
    Dim iEvilZero As Integer
   
    'Store 432 in iNumber
    iNumber = 432
   
    ' Store zero in iEvilZero
    iEvilZero = 0
   
    ' When an error occurs, jump to the divideError label.
    On Error GoTo divideError
   
    ' Divide iNumber by iEvilZero and store the result in iNumber
    iNumber = iNumber / iEvilZero
   
divideError:
    ' Display a polite message box with a critical icon
    MsgBox "Sorry, I can't divide by zero.", vbCritical, "Error"

    ' Close the program (this error could be critical)
    End
End