Generates a run-time error; can be used instead of the Error statement.
Public Sub Raise( _ ByVal Number As Integer, _ Optional ByVal Source As Object = Nothing, _ Optional ByVal Description As Object = Nothing, _ Optional ByVal HelpFile As Object = Nothing, _ Optional ByVal HelpContext As Object = Nothing _ )
vbObjectError + 513
to the Number property.Exception type | Error number | Condition |
---|---|---|
5 | Number is greater than 65535. |
All of the Raise arguments except Number are optional. If you omit optional arguments, and the property settings of the Err object contain values that have not been cleared, those values serve as the values for your error.
Raise is useful for generating errors when writing class modules, because the Err object gives richer information than when you generate errors with the Error statement. For example, with the Raise method, the source that generated the error can be specified in the Source property, online Help for the error can be referenced, and so on.
This example uses the Err object's Raise method to generate an error within a function written in Visual Basic. The calling function can catch the error and report it to the user with a message box.
Const WidthError = 1 Const WidthHelp = 101 Function TestWidth(ByVal width As Integer) If width > 1000 Then Err.Raise(
vbObjectError + 512 + WidthError, "TestWidth", _ "Width must be less than 1000.", "HelpFile.hlp", WidthHelp)
End If End Function ' Add to calling function. Try TestWidth(2000) Catch ex As Exception MsgBox(ex.Message) End Try
Clear Method | Description Property | Err Object | Error Statement | HelpContext Property | HelpFile Property | LastDLLError Property | Number Property | On Error Statement | Source Property |