Raise Method (Err Object)

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 _
)

Parameters

  • Number
    Required. Long integer that identifies the error. Visual Basic errors are in the range 0–65535; the range 0–512 is reserved for system errors; the range 513–65535 is available for user-defined errors as well. However, when you set the Number property for an error that you are creating, add your error code number to the vbObjectError constant. For example, to generate the error number 1000, assign vbObjectError + 1000 to the Number property.

  • Source
    Optional. String expression naming the object or application that generated the error. When setting this property for an object, use the form project.class. If Source is not specified, the process ID of the current Visual Basic project is used.

  • Description
    Optional. String expression describing the error. If unspecified, the value in the Number property is examined. If it can be mapped to a Visual Basic run-time error code, the string that would be returned by the Error function is used as the Description property. If there is no Visual Basic error corresponding to the Number property, the "Application-defined or object-defined error" message is used.

  • HelpFile
    Optional. The fully qualified path to the Help file in which help on this error can be found. If unspecified, Visual Basic uses the fully qualified drive, path, and file name of the Visual Basic Help file.

  • HelpContext
    Optional. The context ID identifying a topic within HelpFile that provides help for the error. If omitted, the Visual Basic Help-file context ID for the error corresponding to the Number property is used, if it exists.

Exceptions

Exception type

Error number

Condition

ArgumentException

5

Number is greater than 65535.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

Remarks

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.

Because the Err object gives richer information than when you generate errors with the Error statement, Raise is useful for generating errors when writing class modules. 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.

Example

This example uses the Err object's Raise method to generate an original error within a function written in Visual Basic. The calling function can catch the error and report it to the user. Notice that procedure CallingProcedure contrasts the type of information that you can derive from an Err object with the information that you can derive from an Exception object.

Module Module1

    Const WidthErrorNumber As Integer = 1000
    Const WidthHelpOffset As Object = 100

    Sub Main()
        CallingProcedure()
    End Sub 

    Sub TestWidth(ByVal width As Integer)
        If width > 1000 Then 
            ' Replace HelpFile.hlp with the full path to an appropriate 
            ' help file for the error. Notice that you add the error  
            ' number you want to use to the vbObjectError constant.  
            ' This assures that it will not conflict with a Visual 
            ' Basic error.
            Err.Raise(vbObjectError + WidthErrorNumber, "ConsoleApplication1.Module1.TestWidth", _
            "Width must be less than 1000.", "HelpFile.hlp", WidthHelpOffset)
        End If 
    End Sub 

    Sub CallingProcedure()
        Try 
            ' The error is raised in TestWidth.
            TestWidth(2000)
        Catch ex As Exception
            ' The Err object can access a number of pieces of 
            ' information about the error.
            Console.WriteLine("Information available from Err object:")
            Console.WriteLine(Err.Number)
            Console.WriteLine(Err.Description)
            Console.WriteLine(Err.Source)
            Console.WriteLine(Err.HelpFile)
            Console.WriteLine(Err.HelpContext)
            Console.WriteLine(Err.GetException)

            Console.WriteLine(vbCrLf & "Information available from Exception object:")
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.ToString)

            Err.Clear()
        End Try 
    End Sub 
End Module 

' The example produces the following output: 
' Information available from Err object: 
' -2147220504 
' Width must be less than 1000. 
' ConsoleApplication1.Module1.TestWidth 
' HelpFile.hlp 
' 100 
' System.Exception: Width must be less than 1000. 
'    at Microsoft.VisualBasic.ErrObject.Raise(Int32 Number, Object Source, Object 
' Description, Object HelpFile, Object HelpContext) 
'    at ConsoleApplication1.Module1.TestWidth(Int32 width) in C:\Users\sahnnyj\App 
' Data\Local\Temporary Projects\ConsoleApplication1\Module1.vb:line 17 
'    at ConsoleApplication1.Module1.CallingProcedure() in C:\Users\sahnnyj\AppData 
' \Local\Temporary Projects\ConsoleApplication1\Module1.vb:line 25 
' 
' Information available from Exception object: 
' Width must be less than 1000. 
' System.Exception: Width must be less than 1000. 
'    at Microsoft.VisualBasic.ErrObject.Raise(Int32 Number, Object Source, Object 
' Description, Object HelpFile, Object HelpContext) 
'    at ConsoleApplication1.Module1.TestWidth(Int32 width) in C:\Users\sahnnyj\App 
' Data\Local\Temporary Projects\ConsoleApplication1\Module1.vb:line 17 
'    at ConsoleApplication1.Module1.CallingProcedure() in C:\Users\sahnnyj\AppData 
' \Local\Temporary Projects\ConsoleApplication1\Module1.vb:line 25

Requirements

Namespace: Microsoft.VisualBasic

**Module:**ErrObject

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Reference

Err Object (Visual Basic)

Clear Method (Err Object)

Description Property (Err Object)

Error Statement

ErrorToString Function

HelpContext Property (Err Object)

HelpFile Property (Err Object)

LastDllError Property (Err Object)

Number Property (Err Object)

On Error Statement (Visual Basic)

Source Property (Err Object)

ArgumentException