Page 58 - Computing Book 8
P. 58

Developing Applications – Visual Studio                                              Class 8


               Program Debugging and Error Handling:

               Error handling is an essential procedure in Visual Basic 2010 programming that helps make a
               program error-free. The user does not have to face all sorts of problems such as program crashes or
               system hangs. Errors often occur due to incorrect input from the user. For example, the user might
               make the mistake of attempting to enter text (string) to a box that is designed to handle only
               numeric values such as the weight of a person, the computer will not be able to perform the
               arithmetic calculation for text, therefore, will create an error. These errors are known as
               synchronous errors.

               In this example, we will deal with the error of entering non-numeric data into text boxes that are
               supposed to hold numeric values. The program_label here is error_hanldler. When the user enters
               non-numeric values into the textbox, the error message will display the text “One of the entries is
               not a number! Try again!” If no error occurs, it will display the correct answer.

               Follow the steps below to create a program which can handle user input errors:
                   •  Create a new form with five labels, two text boxes, and a button.
                                            st
                   •  Change the text of the 1  label to First Number
                                            nd
                   •  Change the text of the 2  label to Second Number
                                            rd
                   •  Change the text of the 3  label to “Division of First
                       Number with Second Number” and also change its size
                       by Maximum Size properties under Layout heading
                       height=20 and width = 120
                                            th
                   •  Change the text of the 4  label to Result and change its
                       name to Lbl_Answer
                                          th
                   •  Clear the text of the 5  label to hide it while the program
                       is running and name it Lbl_ErrorMsg

                   •  Change the text of the Button to Calculate and also
                       change its name to CmdCalculate
                                              st
                   •  Change the name of the 1  text box to FirstNumber
                                              nd
                   •  Change the name of the 2  text box to SecondNumber
                   •  Double click on the button to open script window and
                       type this code

                                                                                                Hiding the Error
                                                                                                 Message box
                                                                                               Declaration of error
          Simple Program for                                                                   handing that if error
           division of two                                                                     occurs go to error
             numbers                                                                               handler

                                                                                                  Response of error
                                                                                                handler in case of error.
                                                                                                  Result Label will
                                                                                                 displays “Error” and
                                                                                                 ErrorMsg box which
                                                                                                was hidden in the first
                                                                                                 line of code will be
                                                                                                 visible with the Error
                                                                                                   Message in “ ”




               The City School /Academics/Computing Curriculum/Class 8/2020-2021                Page 57 of 75
   53   54   55   56   57   58   59   60   61   62   63