There are three main types of errors that could occur while your application is being used:
One of the best qualities of an effective programmer is to anticipate as many problems as possible and to deal with them in the early stages. Some problems can be easy to fix. With some others, you will simply need more experience to know how to fix them. Unfortunately, it will not be unusual to have users asking you to fix your application when a problem may not come from it.
Most or early errors occur in your code. If you are using the IDE to write your code, it can help you detect syntax errors and fix them. If you are using Notepad and the free vbc compiler as we have done so far, you can start by paying attention to your code. When you think everything is fine, compile your code. If there is a syntax error, the compiler will let you know. If there is no syntax error, the compilation will be over and the executable will be ready. You can then execute the application to see the result. If the user is not asked to provide value(s), you are less likely to get a run-time error. A run-time error is one that occurs when using your application. Suppose you write the following code: Module Exercise Public Function Main() As Integer Dim Number As Double Dim Twice As Double Console.Write("Enter a number: ") Number = Console.ReadLine() Twice = Number * 2 Console.WriteLine("{0} * 2 = {1}", Number, Twice) Console.WriteLine() Return 0 End Function End Module Here is an example of executing it: Enter a number: 246.88 246.88 * 2 = 493.76 The first thing your should do is to imagine what could cause a problem. If you think there is such a possibility, start by creating a label that could be used to transfer code if a problem occurs. Here is an example: Module Exercise Public Function Main() As Integer Dim Number As Double Dim Twice As Double Console.Write("Enter a number: ") Number = Console.ReadLine() Twice = Number * 2 Console.WriteLine("{0} * 2 = {1}", Number, Twice) Console.WriteLine() ThereWasAProblem: Console.WriteLine("There was a problem when executing your instructions") Return 0 End Function End Module As we saw when we introduced labels, if you create one, you should tell the compiler when to jump to that label. Otherwise, as in this case, the label section would always execute. Here is an example of running the above version: Enter a number: 12.46 12.46 * 2 = 24.92 There was a problem when executing your instructions In this case, we want the label section to execute only when we want it to. To prevent the compiler from reaching this section if not directed so, you can add an Exit Sub line above the label section: Module Exercise Public Function Main() As Integer Dim Number As Double Dim Twice As Double Console.Write("Enter a number: ") Number = Console.ReadLine() Twice = Number * 2 Console.WriteLine("{0} * 2 = {1}", Number, Twice) Console.WriteLine() Exit Sub ThereWasAProblem: Console.WriteLine("There was a problem when executing your instructions") Return 0 End Function End Module This time if you execute the program with an appropriate value, the label section would not be reached.
The above program will compile fine. When executing it, imagine that the user types an inappropriate value such as 24$.58 instead of 244.58. In this case, the value is not a number and the program would "crash" and let you know that there was a problem. If you have some experience, you would know what the problem was, otherwise, you would face a vague explanation. The short story is that the compiler couldn't continue because, in this case, it could not multiply 24$.58 by 2. 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 procedure. 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 procedure. Here is an example: Module Exercise Public Function Main() As Integer Dim Number As Double Dim Twice As Double Console.Write("Enter a number: ") Number = Console.ReadLine() Twice = Number * 2 Console.WriteLine("{0} * 2 = {1}", Number, Twice) Console.WriteLine() Exit Sub Err_Main: Console.WriteLine("In Main(): The operation could not be executed") Return 0 End Function End Module When you think there will be a problem in your code, somewhere in the lines under the name of the procedure 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: Module Exercise Public Function Main() As Integer On Error GoTo Err_Main Dim Number As Double Dim Twice As Double Console.Write("Enter a number: ") Number = Console.ReadLine() Twice = Number * 2 Console.WriteLine("{0} * 2 = {1}", Number, Twice) Console.WriteLine() Exit Sub Err_Main: Console.WriteLine("In Main(): The operation could not be executed") Return 0 End Function End Module Here is an example of running the program: Enter a number: 24$.58 In Main(): The operation could not be executed 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 procedure is executed, it should 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 procedure. |
|
|||||||||||||||||||
|
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 numbered label instead of a lettered label. Here is an example: Module Exercise Public Function Main() As Integer On Error GoTo 624 Dim Number As Double Dim Twice As Double Console.Write("Enter a number: ") Number = Console.ReadLine() Twice = Number * 2 Console.WriteLine("{0} * 2 = {1}", Number, Twice) Console.WriteLine() Exit Function 624: Console.WriteLine("In Main(): The operation could not be executed") Return 0 End Function End Module Here is an example of running the program: Enter a number: 25G In Main(): The operation could not be executed Press any key to continue . . .
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 procedure. If this happens, as mentioned above, when the compiler returns where the problem occurred, you can provide an alternate. 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: Module Exercise Public Function Main() As Integer On Error GoTo Err_Main Dim Number As Double Dim Twice As Double Console.Write("Enter a number: ") Number = Console.ReadLine() Resume Twice = Number * 2 Console.WriteLine("{0} * 2 = {1}", Number, Twice) Console.WriteLine() Exit Sub Err_Main: Console.WriteLine("In Main(): The operation could not be executed") Return 0 End Function End Module When an error occurs, if you want the program to continue with with an alternate value than the one that caused the problem, in the label section, type Resume Next. Here is an example: Module Exercise Public Function Main() As Integer On Error GoTo Err_Main Dim Number As Double Dim Twice As Double Console.Write("Enter a number: ") Number = Console.ReadLine() Resume Twice = Number * 2 Console.WriteLine("{0} * 2 = {1}", Number, Twice) Console.WriteLine() Exit Sub Err_Main: Console.WriteLine("In Main(): The operation could not be executed") Resume Next Return 0 End Function End Module 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. Here is an example of running the above program: Enter a number: 24$.58 In Main(): The operation could not be executed 0 * 2 = 0 Based on this, you can provide a new value to use in case of error. Here is an example: Module Exercise Public Function Main() As Integer On Error GoTo Err_Main Dim Number As Double Dim Twice As Double Console.Write("Enter a number: ") Number = Console.ReadLine() Twice = Number * 2 Console.WriteLine("{0} * 2 = {1}", Number, Twice) Console.WriteLine() Exit Sub Err_Main: Console.WriteLine("Invalid Number: The value you provided is inappropriate") Console.WriteLine("10 will be used instead") Number = 10 Resume Next Return 0 End Function End Module Here is one example of running the program: Enter a number: 244.58 244.58 * 2 = 489.16 Here is another example of running the same program: Enter a number: 24$.58 Invalid Number: The value you provided is inappropriate 10 will be used instead 10 * 2 = 20
To support error handling, Visual Basic 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. |
|
|||||||
|