Home -> Programming -> Processing -> Error Trapping

Error Trapping

The concept of Error Trapping is important in programming. We need the user to enter data into our program so we can process it and output the results. There is an acronym which relates to this which has been around since the early years of computing. GIGO stands for garbage in garbage out. This refers to the fundamental notion that if the information entering our programs doesn't make sense (is garbage) then the output of our programs must necessarily also be garbage!

Error Trapping helps us avoid this problem by checking that information coming into our program conforms to some minimal level of accuracy. While this may not eliminate all sources of error, it will improve the functioning and the reliability of our programs. What follows are three example of increasing complexity which will demonstrate how Error Trapping can be done.

Passive Error Trapping

In this case we check the data but we only tell the user that a data entry problem exists, we do not use the data for any processing. If we ask the user to enter their name, we cannot be sure that they have entered their own real name. However, we can check to see whether any data at all has been entered; whether more than one character has been entered; and whether the data that has been entered is numbers or text. Let's say we have a textbox named txtFirstName which we expect to hold a users first name. Here is how we would do this. Since we are checking for three possible errors there will need to be three statements, one for each thing we want to check.

If txtFirstName.Text = "" then
     MessageBox.Show("Please enter your name, you cannot leave this field blank.", "Data Entry Error", vbOKOnly, vbIcons.Stop)
If Len(txtFirstName.Text) = 1 then
     MessageBox.Show("Please enter your name and not just an initial.", "Data Entry Error", vbOKOnly, vbIcons.Stop)
if IsNumeric(txtFirstName.Text) then
     MessageBox.Show("Please enter your name and not a number.", "Data Entry Error", vbOKOnly, vbIcons.Stop)


These three lines of code show how to test for the three conditions specified above and inform the user about the results of our checking. Remember the primary purpose of Error Trapping is to prevent our program from crashing due to inappropriate data, not to police the users actions, so passive Error Trapping is not the best thing we can do. We must also remember as programmers to be polite in pointing errors, not insulting.

Active Error Trapping

Here we will check the data and make our processing conditional on passing the tests performed. We normally do this to prevent our program form crashing because inappropriate data was entered by the user. We have no idea whether the inappropriate data entry was an innocent mistake, an attempt to debug a program, or an intentional attempt to crash the program for unknown purposes. In this example, information is entered which we need to perform a calculation. We cannot add, subtract, multiply or divide with anything other than numerical data. Therefore one of the safeties which we should employ is to check all data which is supposed to be a number to make sure that it is. If we try to calculate with data which is not numerical our program will crash! A crashing program is one of the worst things we can do to our users! Here is how we would trap for data to be used in adding two numbers:

If IsNumeric(txtNum1.Text) and IsNumeric(txtNum2.Text) then
     intRresult = CInt(txtNum1.Text) + CInt(intNum2.Text)
else
     MessageBox.Show("You must enter numbers in both fields, text cannot be added.", "Data Entry Error", vbOKOnly, vbIcons.Stop)
EndIf

Notice here that I set up the Error Trapping so that two and only two outcomes are possible. Either both fields contain numbers, in which case we add them; or one or both fields do not contain numbers, in which case we inform the user of this fact. We are also separating hte calculation from assigning the result into an outpust field.
Note that the CInt function used converts the data in the field into integers while the IsNumeric function only checks to see if the data can be converted into numbers. We can change this example to also make sure that the fields are not blank as follows.

If txtNum1.Text = "" or txtNum2.Text = "" then
     messageBox.Show("You cannot leave either field blank!.", "Data Entry Error", vbOKOnly, vbIcons.Stop)
ElseIf Not (IsNumeric(txtNum1.Text) and IsNumeric(txtNum2.Text)) then
     messageBox.Show("You must enter numbers in both fields, text cannot be added.", "Data Entry Error", vbOKOnly, vbIcons.Stop)
Else
     intRresult = CInt(txtNum1.Text) + CInt(intNum2.Text)
EndIf


Here we first test to see if either of the two number fields to be added are blank (contain no data). If this is true then we inform the user of this fact. If and only if the fields are not blank we perform the next check to see if the data in either field is not numeric (the negative form of 'is text'). If it is not we so inform the user. If and only if we pass both tests then we perform the calculation. If we were to place the calculation after the EndIf then it would be performed whether or not the data was blank or numeric. This would cause the very thing we are trying to prevent! The lesson here is simple: Always perform calculations inside the Else clause of the Error Trapping structure.

A Complex Example of Active Error Trapping

0Not Used
1Maritimes
2Quebec
3Quebec
4Ontario
5Ontario
6 Prairies
7British Columbia
8Territories
9Visa Holder
A third example is the validation of a piece of data to see if it conforms to a specific standard. In Canada you must have a SIN - Social Insurance Number in order to work. SINs are provided by the government who devised a scheme to provide minimal checking to see if a particular number is a valid SIN. It goes like this:

A SIN is comprised of nine numbers. The first number reveals where the individual resided, as shown in the table to the right, when they applied for the number. The next seven numbers are for all intents and purposes random. The last number is a check sum number which must be set to a particular value for the number to be a valid SIN number. The check sum is calculated as follows:

Action / Digit123456789Result
Add the Odd Digits1+3+5+7=16
Social Insurance Number123456789
Multiply the Even Digits by 2481216
Convert to Single Digit by Adding and then Add the Digits4+8+3+7+22
Add Even & Odd Totals38

At this point the units digit of the result is subtracted from 10. The difference is the value of the checksum. So the SIN as shown in the table above is invalid. The valid SIN would be 123456782. Now how would a person code this? I will show this using simple variables and then using arrays. Both are equally effective, judge for yourself which is better. The SIN data is contained in the control txtSIN.Text and the result of the validity test is displayed in the control lblValid.Text.

Simple VariablesArrays
Dim int1, int2, int3, int4, int5, int6, int7, int8, int9 as Integer
Dim oddTotal, evenTotal, checkTotal as integer
Dim checkDigit as integer

'Error Trapping starts here
If txtSIN.Text = "" then
     MessageBox.Show("Please enter the Social Insurance Number.", "Data Entry Error", vbOKOnly, vbIcons.Stop)
ElseIf Len(txtSIN.Text)<> 9 then
     MessageBox.Show("Number entered is not 9 digits.", "Data Entry Error", vbOKOnly, vbIcons.Stop)
Else
     'code to check SIN validity starts here
     int1 = Cint(Mid(txtSIN.Text, 1, 1))
     int2 = Cint(Mid(txtSIN.Text, 2, 1))
     int3 = Cint(Mid(txtSIN.Text, 3, 1))
     int4 = Cint(Mid(txtSIN.Text, 4, 1))
     int5 = Cint(Mid(txtSIN.Text, 5, 1))
     int6 = Cint(Mid(txtSIN.Text, 6, 1))
     int7 = Cint(Mid(txtSIN.Text, 7, 1))
     int8 = Cint(Mid(txtSIN.Text, 8, 1))
     int9 = Cint(Mid(txtSIN.Text, 9, 1))
     oddTotal = int1 + int3 + int5 + int7
     int2 = int2 *2
     if int2 > 9 then int2 = int2 - 9
     int4 = int4 *2
     if int4 > 9 then int4 = int2 - 9
     int6 = int6 *2
     if int6 > 9 then int6 = int2 - 9
     int8 = int8 *2
     if int8 > 9 then int8 = int2 - 9
     evenTotal = int2 + int4 + int6 = int8
     checkTotal = oddTotal + evenTotal
     checkDigit = 10 - (checkTotal mod 10)
     If checkDigit = 10 then checkDigit = 0
     If checkDigit = digit(9) then
          lblValid.Text = "True"
     Else
          lblValid.Text = "False"
     EndIf
EndIf
Dim digits(1..9) as integer
Dim oddTotal, evenTotal, checkTotal as integer
Dim checkDigit as integer
Dim index as integer

'Error Trapping starts here
If txtSIN.Text = "" then
     MessageBox.Show("Please enter the Social Insurance Number.", "Data Entry Error", vbOKOnly, vbIcons.Stop)
ElseIf Len(txtSIN.Text)<> 9 then
     MessageBox.Show("Number entered is not 9 digits.", "Data Entry Error", vbOKOnly, vbIcons.Stop)
Else
     'code to check SIN validity starts here
     For index = 1 to 8
          digit(index) = CInt(Mid(txtSIN.Text, index, 1))
          If index mod 2 = 0 then
               digit(index) = digit(index *2)
               if digit(index) > 9 then
                    digit(index) = digit(index) - 9
               EndIf
               evenTotal = evenTotal + digit(index)
          Else
               oddTotal = oddTotal + digit(index)
          EndIf
     Next index
     checkTotal = oddTotal + evenTotal
     checkDigit = 10 - (checkTotal mod 10)
     if checkDigit = 10 then checkDigit = 0
     If checkDigit = digit(9) then
          lblValid.Text = "True"
     Else
          lblValid.Text = "False"
     EndIf
EndIf

As you can see the tradeoff is between simple but lengthy on the left, and efficient but more complex on the right. As our programs become longer, we need the efficiency to reduce the length of our programs and improve the reliability of our code. However, too complex has the disadvantage of making it harder to understand and sometimes harder to code correctly.