In Visual Basic programming, effective error handling is a crucial practice to ensure the seamless operation of a program. By addressing errors, programmers can significantly enhance the program's reliability and performance. A smoothly running, error-free program minimizes the likelihood of issues like program crashes or system hang-ups, offering users a hassle-free experience. Common errors often stem from inaccurate user inputs. For instance, attempting to instruct the computer to divide a number by zero is a classic mistake that leads to a system error. Similarly, inputting text (string) into a field designed exclusively for numeric values, such as a person's weight, can disrupt the program's ability to perform arithmetic calculations, resulting in an error. These types of errors, referred to as synchronous errors, highlight the importance of robust error-handling mechanisms in Visual Basic programming..
Therefore a good programmer should be more alert to the parts of the program that could trigger errors and should write errors handling code to help the user in managing the errors. Writing errors handling code should be considered a good practice for Visual Basic programmers, so do not try to finish a program fast by omitting the errors handling code. However, there should not be too many errors handling code in the program as it creates problems for the programmer to maintain and troubleshoot the program later.
We shall now learn how to write errors handling code in Visual Basic. The syntax for errors handling is
On Error GoTo program_label
where program_label is the section of code that is designed by the programmer to handle the error committed by the user. Once an error is detected, the program will jump to the program_label section for error handling.
Private Sub CmdCalculate_Click() Dim firstNum, secondNum As Double firstNum = Txt_FirstNumber.Text secondNum = Txt_SecondNumber.Text On Error GoTo error_handler Lbl_Answer.Caption = firstNum / secondNum Exit Sub 'To prevent error handling even the inputs are valid error_handler: Lbl_Answer.Caption = "Error" Lbl_ErrorMsg.Visible = True Lbl_ErrorMsg.Caption = "You attempt to divide a number by zero!Try again!" End Sub Private Sub Txt_FirstNumber_GotFocus() Lbl_ErrorMsg.Visible =False End Sub Private Sub Txt_SecondNumber_GotFocus() Lbl_ErrorMsg.Visible = False End Sub
In this example, you design the interface as shown in Figure 35.1 below. Name the first textbox as Txt_FirstNumber and the second textbox as Txt_SecondNumber. Insert one command button as label it as Calculate. Insert another label and name it as Lbl_Answer to display the answers. If the user enter 0 in the second textbox as shown above, the program will jump to the label error_handler, and the error handling procedure is executed. It will show an error in the Txt_Answer label and an error message in the Lbl_ErrorMsg label.
Notice that Exit sub after the division. It is to prevent the program from executing the error_handler code even though the user does not enter zero in the second textbox.Lastly, after the error message appeared, the user will click on the textboxes again. When this occur, the error message will disappear both from the answer label and error message label. This is achieved by using the event procedure GotFocus, as shown in the code.
By referring to Example 35.1, we need to consider other types of errors made by the user, such as entering non-numeric inputs like letters. Therefore, we need to write error handling code for this error too. It should be put in the first place as soon as the user input something in the textboxes. And the error handler label error_handler1 for this error should be put after the error_handler2 label. This means the second error handling procedure is nested within the first error handling procedure. Notice that you have to put an Exit Sub for the second error handling procedure to prevent to execute the first error handling procedure again.
Private Sub CmdCalculate_Click() Dim firstNum, secondNum As Double On Error GoTo error_handler1 firstNum = Txt_FirstNumber.Text secondNum = Txt_SecondNumber.Text On Error GoTo error_handler2 Lbl_Answer.Caption = firstNum / secondNum Exit Sub 'To prevent errror handling even the inputs are valid error_handler2: Lbl_Answer.Caption = "Error" Lbl_ErrorMsg.Visible = True Lbl_ErrorMsg.Caption ="You attempt to divide a number by zero!Try again!" Exit Sub error_handler1: Lbl_Answer.Caption = "Error" Lbl_ErrorMsg.Visible = True Lbl_ErrorMsg.Caption ="You are not entering a number! Try again!" End Sub Private Sub Txt_FirstNumber_GotFocus() Lbl_ErrorMsg.Visible = False End Sub Private Sub Txt_SecondNumber_GotFocus() Lbl_ErrorMsg.Visible = False End Sub