Inline Error Handling

You may be used to programming in a language that doesn’t interrupt the execution of your code with exceptions when errors occur, but instead records errors for you to check and handle later. For example, the C programming language works this way. You may sometimes find it convenient to follow this practice in your Visual Basic code. When you check for errors immediately after each line of code that may cause an error, you are performing inline error handling.

The simplest way to return an error number to a calling procedure is to create functions and statements that return an error number instead of a value if an error occurs. Using this method, you implement inline error handling by testing for an error immediately after each statement or function call in your code. In each case, you can design an error handler that tests for just the kind of error that may arise and provides an appropriate resolution.

This approach doesn’t require that an actual Visual Basic run-time error occur. You can also trap user-defined errors, or you can use this method when you’re working with DLLs or other libraries that don’t raise Visual Basic errors.

The following example uses the DoubleNum function from a section earlier in this chapter to demonstrate how to return an error number to a calling procedure if an argument passed to the DoubleNum function isn’t numeric.

Dim TestValue As Variant

TestValue = DoubleNum("3210r")
If IsError(TestValue) Then
	.
	. ' Handle the error.
	.
Else
	.
	. ' Continue program.
	.
End If

You can also use the Raise method of the Err object to pass an error value to the calling procedure. Place the On Error Resume Next statement at the beginning of your procedure, or before statements or function calls in your code that may cause an error. After an error occurs, you can use error-handling code to examine the value returned by the Err object’s properties. If the Number property of the Err object doesn’t return zero, an error has occurred and the error-handling code can take the appropriate action.

The example in the previous section can be modified to use the Raise method as follows:

Function DoubleNum(Num)
	If IsNumeric(Num) Then
		DoubleNum = Num * 2					' Return result.
	Else
		Err.Raise Number:=30000			' Raise a user-defined error.
	End If
End Function

	Dim TestValue As Variant

	On Error Resume Next
	TestValue = DoubleNum("3210r")
	If Err.Number = 30000 Then
		.
		. ' Handle the error.
		.
	Else
		.
		. ' Continue program.
		.
	End If
	On Error GoTo 0

There are times when you may need to defer error handling in a loop. You can do this by manually resetting the Number property of the Err object to zero so that you can continue to check for errors in the loop. For example, the following Sub procedure iterates through all of the fields in a recordset and tries to change their values. If an error occurs, the field name is printed in the Debug window.

Sub SetAllFields(rst As Recordset)
	Dim fld As Field

	rst.MoveFirst
	On Error Resume Next
	Do Until rst.EOF
		For Each fld In rst.Fields
			rst.Edit
				fld = InputBox("New value for " & fld.Name & "?", , "New Value")
				If Err <> 0 Then
					Err = 0
					Debug.Print fld.Name
				End If
			rst.Update
		Next fld
		rst.MoveNext
	Loop
	rst.Close
End Sub

If you don’t set the Number property of the Err object equal to zero inside the loop, then the first error that occurs will cause every subsequent field’s name to be printed in the Debug window.

Keep these points in mind when using inline error handling: