|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Testing Your ProgramsBefore you release your programs for others to use, youll probably want to test them first. This can involve a large investment of timeone that programmers are reluctant to make. It helps if youre 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 rangesits 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 doesnt 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, youll 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. Its 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 usualand unusualoperating circumstances, the more confidence youll have that things are going as they should. Thats it for the overview of whats in this chapternow its time to turn to the Immediate Solutions section. Immediate SolutionsWriting Error HandlersVisual 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. Lets see an example to make this clearer. One area of programming very susceptible to runtime errors is file handling; well write our example here to open a file and display its contents in a text boxas 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 reasonsfor 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, weve used very rudimentary code in our error handler, but error handlers get much more complex. As well 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 objects 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 specificallythe case where the user clicked the Cancel button in the Common Dialog (you must set the Common Dialog controls 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
Well see how to write error handlers like this oneand see what statements like Resume doin this chapter. The code for this example is located in the errors folder on this books accompanying CD-ROM.
|
|
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. |