O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 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 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required