Name
Err.Clear Method
Syntax
Err.Clear
Description
Explicitly resets all the properties of the Err object after an error has been handled.
Rules at a Glance
You need to clear the Err object only if you need to reference
its properties for another error within the same subroutine or
before another On Error Resume Next 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 IfProgramming Tips and Gotchas
Resetting the Err object explicitly using the Clear method is necessary when you use
OnErrorResumeNextand test the value ofErr.Numberrepeatedly. 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 an
OnErrorResume NextorOn Error Goto 0statement is executed.It is also possible to set the Err.Number property to 0 instead of calling up the Err.Clear method. However, this doesn’t reset the remaining properties of the Err object.
When testing the value of
Err.Number, don’t forget that OLE servers often return “negative” numbers. Actually internally they’re not really negative, but are unsigned longs. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access