Clear Method

Applies To

Err Object.

Description

Clears all property settings of the Err object.

Syntax

object.Clear

The object is always the Err object.

Remarks

Use Clear to explicitly clear the Err object after an error has been handled. This is necessary, for example, when you use deferred error handling with On Error Resume Next. Visual Basic for applications calls the Clear method automatically whenever any of the following statements is executed:

  • Any type of Resume statement.
  • Exit Sub, Exit Function, Exit Property.
  • Any On Error statement.

Clear is equivalent to Err = 0 in previous Basic products.

Note The On Error Resume Next construct may be preferable to On Error GoTo when dealing with errors generated during access to other objects. Checking Err after each interaction with an object removes ambiguity about which object your code was accessing when the error occurred. Thus, you can be sure which object placed the error code in Err.Number, as well as which object originally generated the error (the one specified in Err.Source).

See Also

Description Property, Err Object, HelpContext Property (Visual Basic), HelpFile Property (Visual Basic), LastDLLError Property, Number Property, On Error Statement, Raise Method, Source Property.

Example

This example uses the Err object’s Clear method to reset the numeric properties of the Err object to zero and its string properties to zero-length strings. If Clear were omitted from the following code, the error message dialog box would be displayed on every iteration of the loop (after an error occurs) whether or not a successive calculation generated an error.


Dim Result(10) As Integer            ' Declare array whose elements 
                                    ' will overflow easily.Error Resume Next                    ' Defer error trapping.
Until indx = 10
    ' Generate an occasional error or store result if no error.
    Result(indx) = Rnd * indx * 20000
    If Err.Number <> 0 Then
        MsgBox Err, , "Error Generated: ", Err.HelpFile, Err.HelpContext
        Err.Clear                    ' Clear Err object properties.
    Else
        indx = indx + 1
    End If