9.3 Dealing with Logical ErrorsSince Visual Basic makes the handling of runtime errors a relatively straightforward process, it seems reasonable to try to mimic this process for logical errors. 9.3.1 Detecting Logical ErrorsTo 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. 9.3.2 Where to Handle a Logical ErrorOnce a logical error is detected , we have three choices as to where to handle that error. 9.3.2.1 Handling the error on the spotA logical error can be handled at the location where it was detected. Here is an example: Public Sub GetSomeData( ) TryAgain: DataCt = CInt(InputBox("Enter number of items.")) ' Check for error If DataCt < = 0 then If MsgBox("Number must be a positive integer." & _ " Try again or cancel.", vbQuestion+vbOKCancel) _ = vbOK then Goto TryAgain Else Exit Sub End If End If '' Code here to get the individual data values ... End Sub Handling a logical error on the spot may be appropriate when the required code is short. It is also appropriate in Property procedures, which often amount to little more than a single line that sets a private instance variable, preceded by data validation, which is essentially logical-error detection. 9.3.2.2 Handling the error in the offending procedure's error handlerWe can duplicate the procedure that Visual Basic uses for runtime errors simply by raising our own runtime error. Here is an example using structured exception handling: Try Dim DataCt As Integer = CInt(InputBox("Enter number of items.")) ' Check for error If DataCt <= 0 Then ' Throw an exception Throw New Exception("Must enter a positive number.") End If Catch ex As Exception MsgBox(ex.Message) End Try Note that the Exception class constructor (in one of its overloaded forms) is: Overloads Public Sub New(String) where String is the error message to be associated with the error. Here is an example of error raising using unstructured error handling: Public Sub GetSomeData( ) On Error Goto ErrGetSomeData DataCt = CInt(InputBox("Enter number of items.")) ' Check for error If DataCt < = 0 then ' Raise an error Err( ).Raise Number:= ErrBadDataCt End If ' Code here to get the individual data values ... Exit Sub ' Error-handler ErrGetSomeData: Select Case Err( ).Number Case ErrBadDataCt '' Deal with this error by displaying '' message and getting help from user Case Else '' Deal with other errors End Select Exit Sub End Sub 9.3.2.3 Passing the error to the calling procedureAs with runtime errors, passing the error to the calling procedure can be done in a parameter of the offending procedure or as the return value of the offending function. Also, the calling procedure's error handler can be called by throwing (or raising) an error. |