home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Testing Your Programs

Before you release your programs for others to use, you’ll probably want to test them first. This can involve a large investment of time—one that programmers are reluctant to make. It helps if you’re smart in the way you go about testing your programs. For example, if your program operates on numeric data, you should test the bounds of variable ranges—it’s easy to forget that the limits of Visual Basic integers, which are only 2-byte variables, are –32,768 to 32,767. Entering values like those, or values outside that range, can help test possible danger points. There is a bounds check you can perform for every crucial variable in your program. (Of course, you should check mid-range values as well, because a certain combination of values might give you unexpected errors.)

In addition, file operations are notorious for generating errors. What if the disk is full and you try to write to it? What if the file the user wants to read in doesn’t exist? What if the output file turns out to be read-only? You should address and check all these considerations.

Besides the inherent programming checks, determining the logic danger-points of a 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()
    With CommonDialog1

        .ShowOpen
        Open .FileName For Input As #1
        Text1.Text = Input$(LOF(1), #1)

        Close #1

    End With
End Sub

Errors can occur here for a number of reasons—for example, the user may have typed in the name of a nonexistent file. To handle errors like that, we add an On Error GoTo statement like this, where we indicate that our error handler code will start at the label FileError:

Private Sub Command1_Click()

    On Error GoTo FileError

    With CommonDialog1

        .ShowOpen
        Open .FileName For Input As #1
        Text1.Text = Input$(LOF(1), #1)

        Close #1

    End With

Next, we add that label, FileError, and indicate with a message box that a file error occurred:

Private Sub Command1_Click()

    On Error GoTo FileError

    With CommonDialog1

        .ShowOpen
        Open .FileName For Input As #1
        Text1.Text = Input$(LOF(1), #1)

        Close #1

    End With

FileError:
    MsgBox "File Error"
End Sub

We also have to prevent execution of the normal code continuing into the error handler, so we add an Exit Sub statement to the code before that error handler:

Private Sub Command1_Click()

    On Error GoTo FileError

    With CommonDialog1

        .ShowOpen
        Open .FileName For Input As #1
        Text1.Text = Input$(LOF(1), #1)

        Close #1

    End With

    Exit Sub

FileError:
    MsgBox "File Error"
End Sub

So far, we’ve used very rudimentary code in our error handler, but error handlers get much more complex. As we’ll see in this chapter, you can get the actual error code of the trappable error (as listed in Table 29.1) using the Visual Basic Err object’s Number property. You can make that number the basis of a Select Case statement to take the appropriate action depending on which error occurred.

For example, here we handle two types of errors specifically—the case where the user clicked the Cancel button in the Common Dialog (you must set the Common Dialog control’s CancelError property to True for the Common Dialog control to generate an error when the user clicks the Cancel button) and the File Not Found error:

Private Sub Command1_Click()

    On Error GoTo FileError

    With CommonDialog1

        .ShowOpen
        Open .FileName For Input As #1
        Text1.Text = Input$(LOF(1), #1)

        Close #1

    End With

    Exit Sub

FileError:
    Select Case Err.Number
        Case cdlCancel
            MsgBox "Please select a file."
            Resume
        Case 53
            MsgBox "File not found"
        Case Default
            MsgBox "File Error"
    End Select

End Sub

We’ll see how to write error handlers like this one—and see what statements like Resume do—in this chapter. The code for this example is located in the errors folder on this book’s accompanying CD-ROM.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.