37
Visual Basic 6 Black Book:Error Handling And Debugging
program is also very important. For example, if your program has an array of data and
you let the user average sections of that data by entering the number of cells to average
over, what would happen if the user entered a value of 0? Or –100? Besides testing the
software yourself, releasing beta versions of the software to be tested by other
programmers or potential users is often a good idea.
If you do a lot of programming, you’ll start to feel, sooner or later, that inevitably some
user is going to come up with exactly the bad data set or operation that will crash your
program. You might even start dreading the letters forwarded on to you from the
Customer Relations Department. It’s far better to catch all that before the program goes
out the door, which is what beta testing your software is all about. The longer you test
your program under usual—and unusual—operating circumstances, the more
confidence you’ll have that things are going as they should.
That’s it for the overview of what’s in this chapter—now it’s time to turn to the
Immediate Solutions section.
Immediate Solutions
Writing Error Handlers
Visual Basic has specific built-in ways to handle runtime errors, called trappable
errors. When such an error occurs, you can direct the execution of your program to an
error handler, which is a section of code written specifically to deal with errors.
Let’s see an example to make this clearer. One area of programming very susceptible
to runtime errors is file handling; we’ll write our example here to open a file and
display its contents in a text box—as well as to handle file errors. When the user clicks
a button, Command1, we can show an Open dialog box using a Common Dialog
control, CommonDialog1:
Private Sub Command1_Click()
With CommonDialog1
.ShowOpen
...
End With
End Sub
The user enters the name of the file to open in that dialog box. We open the file, read
the text in the file, and display it a multiline text box with scroll bars, Text1 (with its
Multiline property set to True and its Scrollbars property set to Both); then we close
the file:
Private Sub Command1_Click()
file:///E|/Program%20Files/KaZaA/My%20Shared%...sic%20-%20%20Black%20Book/ch29/1007-1012.html (2 of 5) [7/31/2001 9:06:18 AM]