The On Error Statement

The basic structure of error handling in VB begins with the On Error statement. It diverts program execution in the event of an error, or it switches off error handling in the given procedure.

The On Error statement remains valid while the procedure in which it's defined is in scope, or until another On Error statement is encountered. To explain, let's break this down.

First of all, a procedure is within scope until either an end or exit procedure statement is executed. This means that a procedure is still in scope even when a call is made to another procedure. This has important implications for the On Error statement. For example, let's say you define an error handler in one procedure, and you then call another procedure that doesn't contain an error handler. If an error occurs in the called procedure, the first procedure (and its error handler) is still in scope, so the error is handled by the calling procedure.

The following snippet demonstrates how this works. In this example, an error handler is defined in the Commandl button's Click event handler. A call is made to the FunWithNumbers sub, and the value 0 is passed to it as a parameter. Unfortunately, FunWithNumbers uses this value as the divisor and, since it's illegal to divide by zero, a runtime error is generated. The fun has just gone out of FunWithNumbers, and the function has no error handler to handle the error. However, the Command1_Click event handler is still in scope, so the error is handled by the Command1_Click_Err error handler:

Private Sub Command1_Click()

On Error GoTo Command1_Click_Err FunWithNumbers 0 MsgBox "all ok" Exit Sub

Command1_Click_Err:

MsgBox Err.Number & vbCrLf & Err.Description

End Sub

Private Sub FunWithNumbers(iVal As Integer)

Error Handling in Procedures 97

txtResult.Text = CStr(i / iVal)

End Sub

To locate an error handler, the VB call stack is used. Each time a call is made from a procedure, it's added onto the end of the call stack. If an error occurs in a procedure that doesn't have an error handler, VB looks at the previous procedure in the stack, until it finds an error handler. If no error handler is found, a terminal runtime error is generated, and your program hits the dust.

A word of warning: you should take care when using On Error Resume and Resume Next within a procedure that calls another procedure. If the called procedure—like FunWithNumbers—doesn't contain any error handling, execution resumes with the line of code containing (in the case of Resume) or the line of code immediately following (in the case of Resume Next) the call to the procedure in the original calling routine that contains the error handler. This is illustrated in the following code fragment:

Private Sub Command1_Click()

On Error Resume Next

FunWithNumbers 0 MsgBox "All OK" Exit Sub

End Sub

Private Sub FunWithNumbers(iVal As Integer)

txtResult.Text = CStr(i / iVal)

End Sub

The user would assume that the procedure has worked correctly because all he sees is the "All OK" message; FunWithNumbers hasn't updated the txtResult text box with a value because control didn't return to FunWithNumbers after the error.

An On Error statement, then, remains in effect until the next On Error statement, which can be in the same procedure or in a called procedure. For instance, in the example above, if FunWithNumbers had implemented an error handler, it would have handled the error rather than passing it back up the call stack to the command button's error handler. The following is an example of a procedure that uses multiple On Error statements. In this example, an error handler is defined and immediately activated. However, later in the procedure, a For Each...Next statement is used in a way that most likely will cause an error in normal operation (as would happen, for instance, if a control in the Controls collection doesn't have a Text property), so the original On Error statement is replaced by an On Error Resume Next statement that basically skips past any errors. Once this section of

98 Chapter 6- Error Handling the procedure is complete, the original error handler is switched back on, canceling On Error Resume Next:

Private Function ResetControls() As Boolean

On Error GoTo ResetControls_Err Dim oControl As Control

If Not blnSaved Then

Call ShowWarningMsg Exit Function End If

On Error Resume Next For Each oControl In Controls oControl.Text = ""

Next

On Error GoTo ResetControls_Err

Call DisplayDefaultValues Exit Function

ResetControls_Err:

MsgBox Err.Number & vbCrLf & Err.Description End Function

Note that if an error is generated within an error handler, the error is terminal; Visual Basic doesn't look back up the call stack for another enabled error handler.

The options for error handling within a VB procedure are set using the On Error statement as follows:

On Error 0

Switches off error handling until the next On Error statement (a procedure can contain any number of On Error statements).

On Error Resume

Take care with this one! Program execution continues with the line that caused the error; using it may not be the smartest thing you ever did!

On Error Resume Next

Basically, the error is ignored, and program execution continues with the line following the line containing the error.

On Error Goto label label is the beginning of your error-handling routine within the procedure. A label is a subroutine name followed by a full colon (:).

Was this article helpful?

0 0

Post a comment