Dealing with Logical Errors
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.
Detecting 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 SubThe 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 SubNote that the alternative to immediate detection
of logical errors is to place the error-detecting code just prior to
using the value of DataCt,
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
use DataCt in a variety of locations in the
program, each of which would require error-detecting code.
Where to Handle a Logical Error
Once a logical error is detected, we have three choices as to where to handle that error.
Handling the error on ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access