Error Handling
In the previous example, I have mentioned many times that you can enter correct values before performing computation. However, in actual software use, how can we ensure that users always enter correct data during use?
For example, for numeric values to be calculated, a fatal error may occur if the user does not enter the correct number, which often destroys a program, this is certainly not expected by developers. Therefore, it is very important to take errors seriously. In this chapter, we will look at how VB6 handles errors.
We should first open the project 1 used in the previous step, and we will demonstrate how to handle the error in the form1 form.
1. Error Handling statement
In VB6, there are two types of error handling statements:
L on error goto label
L on error resume next
The following sections describe each other.
(1) on error goto label
For this error handling statement, the on, error, and goto keywords are VB6, And the label is a "label" used to indicate the location of the Code ", it consists of "identifier + colon", such:
Processerror:
The meaning of this error processing statement is: starting from the next code of the statement, if an error occurs, it jumps to the label and continues executing the code after the label. Now we will take Division as an example to describe its usage. Here we can create a button (the caption attribute can be set to "error handling 1") and add the following response code:
Private sub command8_click ()
'Error handling example, On Error Goto label
On Error GoTo subError
Dim n1 As Currency
Dim n2 As Currency
Dim result As Currency
N1 = CCur (Text1.Text)
N2 = CCur (Text2.Text)
Result = n1/n2
Text3.Text = CStr (result)
Exit Sub
SubError:
MsgBox Err. Description
Err. Clear
End Sub
In the code, we define a label "subError ". Then, we use n1 and n2 to save the divisor and divisor for Division calculation, and the result is saved to the result variable. Finally, the result is displayed in Text3.
Think about the possible errors in the Code:
L The content in Text1 cannot be converted to a value correctly.
L The content in text2 cannot be converted to a value correctly.
L The content in text2 is 0, while the divisor cannot be zero.
Now, press F5 to run the program and try out the possible errors to see if there is any difference between them.
Here, we can find that the program may exit when no error processing is added. When an error is added, a prompt window is displayed when an error occurs, is it better for you? In actual development, it is not only possible to convert a number into a value or a value that cannot be zero, but also to determine the value range, format, and other operations. In the future study, we will learn more error handling methods.
Let's look back at the code. Is there any new content in this code.
Here, the exit sub statement is used to exit the current subroutine. In this Code, when there is no error, it is necessary to exit the event processing subroutine after the code that completes normal work is executed. You can delete this line or change it to a comment to see what the result will be. The program will display a prompt window regardless of whether an error occurs.
In addition, we use the ERR Object, which is a system object used to save the last error message. Here we use the description attribute of the ERR Object, which is a text description of the error, the msgbox function can be used to display the error message. After the error message is displayed, the clear method of the ERR Object is used to clear the error information in the ERR Object, so as to prevent incorrect error messages from calling the ERR Object. For more detailed usage of err objects, you can master them in practice. The most common method is the description attribute and clear method.
(2) On Error resume next
The meaning of this statement is very simple, that is, when an error occurs, the program will skip the wrong statement and continue to execute the next one.
Here, I can clearly see its shortcomings. In the division operation in the above example, one of the three possible errors may cause incorrect results, the result is meaningless even if the code is executed. Therefore, it is obviously inappropriate to use this error processing statement in this case of numerical calculation.
Under what circumstances will this error handling statement be used?
I usually use it in some less important operations, for example, you can use it in the method of clearing text box content. In addition, there is no relevance between the pre-and post-operations, and the operation content should not have a potential impact on the final result.
In actual development, we recommend that you use the "on error goto label" statement to handle possible errors, so that you can clearly display the error information to users, so that it can be corrected immediately.
2 error controllability
The division operation in the preceding section is used as an example. Although all three possible errors can be automatically captured by the program, the error information is displayed mechanically, but we can also make it more user-friendly. According to the following code, add the function checkdata in the "common" code segment of the form:
Private function checkdata () as Boolean
Checkdata = false
With text1
If isnumeric (. Text) = false then
Msgbox "enter a correct number"
. Selstart = 0
. SelLength = Len (. Text)
. SetFocus
Exit Function
End If
End
With Text2
If IsNumeric (. Text) = False Then
MsgBox "enter a correct number"
. SelStart = 0
. SelLength = Len (. Text)
. SetFocus
Exit Function
End If
If CCur (. Text) = 0 Then
MsgBox "the divisor cannot be 0. Enter a non-zero value"
. SelStart = 0
. SelLength = Len (. Text)
. SetFocus
Exit Function
End If
End
CheckData = True
End Function
At the beginning of the function, we set the return value of the function to False and start to make possible error judgments.
In the code, we use... The End With statement is not difficult to see from the instance. When the With keyword is followed by a control name, in the code block before the last End With statement, the control name can be omitted for calling the properties and methods of this control, which greatly simplifies code writing.
In the first With block, we use the IsNumeric function to determine whether the Text of Text1 can be converted to a number. If not, a prompt window is displayed, telling you to enter a correct value. Then, use the SelStart and SelLength attributes to set the selected content in Text1. Here, all options are selected. The Len function returns an integer indicating the length of the parameter (here the content of Text1, the number of characters in the Text1 text box.
After all the content of Text1 is selected, we use the SetFocus method of Text1 to set the input focus to the Text1 control and exit the function. In this case, the return value of the function is False, that is, incorrect data input is found during data check.
In the second With block, we first judge whether the content in Text2 is a number, and then add a judgment condition to check whether it is 0, if the divisor is 0, exit the function and return False.
Finally, if no error occurs during data check, the function returns True, indicating that the check is successful.
To test this function, we only need to add the following code to the On Error Goto subError statement in the code in the previous section:
If checkData = False Then Exit Sub
It means that when the check fails, the event will be exited and the following calculation is not performed. At this time, we can run the program and enter different content to see if the program will respond. Does it look very considerate and humane.
It should be noted that although we can handle many predictable errors through a checkdata function, we should not discard the use of the on error GOTO statement, there are some situations that we are not easy to handle in advance, such as when the user inputs a large value.
3 Summary
In this chapter, we understand the error handling statements in VB6 and a method for pre-processing predictable errors, in this way, we can create a relatively stable running process in our own program.
In addition, for error handling, we will find that the processing statement is very simple, but errors in the software still cannot be completely processed. for developers, it is important not only to be familiar with the handling methods in the development environment, but also to fully grasp possible errors and attitudes towards error handling.