Error (statement)

Syntax Error errornumber
Description Simulates the occurrence of the given runtime error.
Comments The errornumber parameter is any Integer containing either a built-in error number or a user-defined error number. The Err function can be used within the error trap handler to determine the value of the error.
Example This example forces error 10, with a subsequent transfer to the TestError label. TestError tests the error and, if not error 55, resets Err to 999 (user-defined error) and returns to the Main subroutine.
Sub Main()
  On Error Goto TestError
  Error 10
  MsgBox "The returned error is: '" & Err() & " - " & Error$ & "'"
  Exit Sub
TestError:
  If Err = 55 Then    'File already open.
    MsgBox "Cannot copy an open file. Close it and try again."
  Else
    MsgBox "Error '" & Err & "' has occurred."
    Err = 999
  End If
  Resume Next
End Sub
See Also Err (statement); Error Handling (topic).