The above program will compile fine. When executing it, imagine that the user types an inappropriate value such as 25$.85 instead of 25.85. In this case, the value is not a number, the program would "crash" and let you know that there was a problem: With some experience, you would know what the problem was, otherwise, you would face a vague explanation. The short story is that the compiler could not continue because, in this case, it could not multiply 25$.85 by another number. If a problem occurs when a person is using your program, the compiler may display a nasty and insignificant message to the user who would not know what to do with it. Therefore, you can start by creating an appropriate label as introduced above. An error normally occurs in a function. Therefore, to make your code easier to read, you should create a label that shows that it is made for an error instead of being a regular label. The label should also reflect the name of the function. Here is an example: Private Sub CalculateClicked(ByVal sender As Object, ByVal e As EventArgs) Handles BtnCalculate.Click Dim Number As Double Dim Result As Double Number = CDbl(TxtNumber.Text) Result = Number * 24 TxtResult.Text = CStr(Result) Exit Sub BtnOperationClickError: MsgBox("The operation could not be executed", MsgBoxStyle.OkOnly, "Operation Error") End Sub When you think there will be a problem in your code, somewhere in the lines under the name of the function but before the line that could cause the problem, type On Error GoTo followed by the name of the label that would deal with the error. Here is an example: Private Sub CalculateClicked(ByVal sender As Object, ByVal e As EventArgs) Handles BtnCalculate.Click On Error GoTo BtnOperationClickError Dim Number As Double Dim Result As Double Number = CDbl(TxtNumber.Text) Result = Number * 24 TxtResult.Text = CStr(Result) Exit Sub BtnOperationClickError: MsgBox("The operation could not be executed", MsgBoxStyle.OkOnly, "Operation Error") End Sub Here is an example of running the program: When the On Error GoTo statement is used, this indicates to the compiler that if any type of error occurs while the code of this function is executed, transfer the compilation to the label. In this case, as soon as something bad happens, the compiler marks the area where the problem occurred, skips the normal code and jumps to the label indicated by the On Error GoTo line. After the section of that label is executed, the compiler returns where the error occurred. If there is nothing to solve the problem, the compiler continues down but without executing the lines of code involved. In this case, it would encounter the Exit Sub line and get out of the function.
Although the label is more explicit, it only indicates to the compiler what line to jump to in case of a problem. The alternative is to specify a line number instead of a label. If a problem occurs in your code and you provide a label to display a friendly message as done above, the compiler would display the message and exit from the function. If this happens, as mentioned above, when the compiler returns where the problem occurred, you can provide an alternative. For example, in our program, if the user provides an inappropriate value that causes the error, you can provide an alternate value and ask the compiler to continue as if nothing happened. In this case, you want to compiler to "resume" its activity. To indicate that the program should continue, you can use the Resume keyword. Here is an example: Private Sub CalculateClicked(ByVal sender As Object, ByVal e As EventArgs) Handles BtnCalculate.Click On Error GoTo BtnOperationClickError Dim Number As Double Dim Result As Double Number = CDbl(TxtNumber.Text) Resume Result = Number * 24 TxtResult.Text = CStr(Result) Exit Sub BtnOperationClickError: MsgBox("The operation could not be executed", MsgBoxStyle.OkOnly, "Operation Error") End Sub When an error occurs, if you want the program to continue with an alternate value than the one that caused the problem, in the label section, type Resume Next. Here is an example: Private Sub CalculateClicked(ByVal sender As Object, ByVal e As EventArgs) Handles BtnCalculate.Click On Error GoTo BtnOperationClickError Dim Number As Double Dim Result As Double Number = CDbl(TxtNumber.Text) Result = Number * 24 TxtResult.Text = CStr(Result) Exit Sub BtnOperationClickError: MsgBox("The operation could not be executed", MsgBoxStyle.OkOnly, "Operation Error") Resume Next End Sub In this case, since any numeric variable is initialized with 0, when the compiler returns to the line of code that caused the problem, it would use 0 as a substitute to the inappropriate value. Based on this, you can provide a new value to use in case of error. Here is an example: Private Sub CalculateClicked(ByVal sender As Object, ByVal e As EventArgs) Handles BtnCalculate.Click On Error GoTo BtnOperationClickError Dim Number As Double Dim Result As Double Number = CDbl(TxtNumber.Text) Result = Number * 24 TxtResult.Text = CStr(Result) Exit Sub BtnOperationClickError: MsgBox("The operation could not be executed", MsgBoxStyle.OkOnly, "Operation Error") Number = 10 Resume Next End Sub Here is one example of running the program: Here is another example of running the same program: To support error handling, the Visual Basic library provides a global variable named Err. This allows you to identify the error and its description. Because an error depends on what caused it and why, the values of the Err variable also depend and are not always the same. |
|
|||||||||||
|