Since Visual Basic makes the handling of runtime errors a relatively straightforward process, it seems reasonable to try to mimic this process for logical errors.
To detect a logical error, we place error-detection code immediately following the potential offender. For instance, consider the following procedure shell for getting a sequence of positive integers from the user, starting with the number of integers:
Public Sub GetSomeData( ) Dim DataCt As Integer DataCt = CInt(InputBox("Enter number of items.")) ' Code here to get the individual data values ... End Sub
The proper place for error-detecting code is immediately following the InputBox function, where we can check for a nonpositive integer:
Public Sub GetSomeData( ) Dim DataCt As Integer DataCt = CInt(InputBox("Enter number of items.")) ' Check for error If DataCt < = 0 then ' something here End If ' Code here to get the individual data values ... End Sub
Note that the alternative to immediate detection
of logical errors is to place the error-detecting code just prior to
using the value of
but this is both dangerous and inefficient. It is dangerous since we
might forget to place the code, and it is inefficient since we may
DataCt in a variety of locations in the
program, each of which would require error-detecting code.
Once a logical error is detected, we have three choices as to where to handle that error.