Err.Clear Method

Syntax

object.Clear


object

Use: Required

Data Type: Error object

An instance of the Err object.

Description

Explicitly resets all the properties of the Err object after an error has been handled.

Rules at a Glance

You need only to Clear the Err object if you need to reference its properties for another error within the same subroutine or before another On Error statement within the same subroutine.

Example

On Error Resume Next
   
i = oObjectOne.MyFunction(iVar)
    
If Err.Number <> 0 Then
   MsgBox "The Error : " & Err.Description & vbCrLf _
          & " was generated in " & Err.Source
   Err.Clear
End If
    
j = oObjectTwo.YourFunction(iVar)
   
If Err.Number <> 0 Then
   MsgBox "The Error : " & Err.Description & vbCrLf _
          & " was generated in " & Err.Source
   Err.Clear
End If

Programming Tips and Gotchas

  • Resetting the Err object explicitly using the Clear method is necessary when you use On Error Resume Next and test the value of Err.Number repeatedly. Unless you reset the Err object, you run the very real risk of catching the previously handled error, the details of which are still lurking in the Err object's properties.

  • The Err object is automatically reset when either a Resume, Exit Sub, Exit Function, Exit Property, or On Error statement is executed.

  • You can achieve the same results by setting the Err.Number property to 0; however your code will be more self-documenting if you use the Clear method.

  • When testing the value of Err.Number, don't forget that OLE servers often return "negative" numbers. Actually internally they're not really negative; they're unsigned longs, but VB has no unsigned long data type. The extra bit of the unsigned long type appears as a "-".

See Also

Err Object, Err.Raise Method, On Error Statement, Resume Statement
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.234.114