|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Using Resume In Error HandlersWhen youre writing code for an error handler, you can return control to the main body of the procedure using the Resume statement. Program execution starts again with the line that caused the error, and this can be very valuable if youre able to fix the error in the error handler. Lets see an example. In this example, we open a file that the user has selected with an Open Common Dialog. If the user clicked the Cancel button instead, we can insist that the user select a file by displaying a message box with the text Please select a file and use the Resume statement to display the Open dialog box once again. We do that by trapping the error generated when the user clicks the Cancel button in the Open dialog box (set the Common Dialog controls CancelError property to True to make sure a trappable error is generated when the user clicks the Cancel button):
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
Using Resume Label In Error HandlersWhen youre writing code for an error handler, you can return control to a particular line in the main body of the procedure using the Resume Label statement. To label a line, you just place the labels text directly into the code, followed by a colon. Lets see an example. Here, we use the Resume Label statement to retry a File Open operation if the user clicked the Cancel button in the Open dialog box. We do this by using the label TryAgain (to make the Open Common Dialog return a trappable error if the user clicks the Cancel button, set the Common Dialog controls CancelError property to True):
Private Sub Command1_Click()
On Error GoTo FileError
With CommonDialog1
TryAgain:
.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 TryAgain
Case 53
MsgBox "File not found"
Case Default
MsgBox "File Error"
End Select
End Sub
Using Resume Label is useful if youre able to fix a trappable error in an error handler and want to resume execution at some specific line in the code (not necessarily the next line in the code). Using Resume line# In Error HandlersYou can use the Resume statement (see the previous two topics) with an actual line number in Visual Basic. For example, heres how wed write the example from the previous topic using line numbers instead of a Resume Label statement:
Private Sub Command1_Click()
1
2 On Error GoTo 16
3
4 With CommonDialog1
5
6 .ShowOpen
7 Open .FileName For Input As #1
8 Text1.Text = Input$(LOF(1), #1)
9
10 Close #1
11
12 End With
13
14 Exit Sub
15
16
17
18 Select Case Err.Number
Case cdlCancel
MsgBox "Please select a file."
Resume 6
Case 53
MsgBox "File not found"
Case Default
MsgBox "File Error"
End Select
End Sub
In this case, were specifying that execution should continue starting at line 6 after weve indicated to the users that they should select a file to open instead of just clicking the Cancel button. Using Resume Next In Error HandlersBesides Resume, Resume Label, and Resume line#, you can also use the Resume Next statement in an error handler. This statement resumes program execution in the line after the one that caused the error. Lets see an example. Here we set the Text, Caption, Min, and Max properties of the currently active control on a form when the user clicks that form. Because no one control has all those properties, this code would generate an error in a message box to the user:
Private Sub Form_Click()
ActiveControl.Text = "Active control"
ActiveControl.Caption = "Active Control"
ActiveControl.Min = 0
ActiveControl.Max = 100
End Sub
We can handle and suppress these errors with an error handler, which we start at the label SetError:
Private Sub Form_Click()
On Error GoTo SetError
ActiveControl.Text = "Active control"
ActiveControl.Caption = "Active Control"
ActiveControl.Min = 0
ActiveControl.Max = 100
Exit Sub
SetError:
...
In this case, we handle the error by moving on to the next line of code like this with Resume Next:
Private Sub Form_Click()
On Error GoTo SetError
ActiveControl.Text = Active control
ActiveControl.Caption = Active Control
ActiveControl.Min = 0
ActiveControl.Max = 100
Exit Sub
SetError:
Resume Next
End Sub
The result here is that whichever of these four properties the active control does have is set, without any errors.
|
|
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. |