ErrObject.Raise(Int32, Object, Object, Object, Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
生成运行时错误;可以使用而不是 Error 语句。
public void Raise(int Number, object? Source = default, object? Description = default, object? HelpFile = default, object? HelpContext = default);
public void Raise(int Number, object Source = default, object Description = default, object HelpFile = default, object HelpContext = default);
member this.Raise : int * obj * obj * obj * obj -> unit
Public Sub Raise (Number As Integer, Optional Source As Object = Nothing, Optional Description As Object = Nothing, Optional HelpFile As Object = Nothing, Optional HelpContext As Object = Nothing)
参数
- Number
- Int32
必填。
Long 用于标识错误性质的整数。 Visual Basic错误在 0-65535 范围内;范围 0-512 保留用于系统错误;范围 513-65535 可用于用户定义的错误。 在类模块中将 Number 属性设置为自己的错误代码时,请将错误代码号添加到 vbObjectError 常量。 例如,若要生成错误号 513,请分配给vbObjectError + 513Number属性。
- Source
- Object
Optional.
String 表达式命名生成错误的对象或应用程序。 为对象设置此属性时,请使用窗体 project.class。 如果未指定 Source,则使用当前Visual Basic项目的进程 ID。
- Description
- Object
Optional.
String 描述错误的表达式。 如果未指定,则会检查属性中的 Number 值。 如果它可以映射到Visual Basic运行时错误代码,Error 函数将返回的字符串用作 Description 属性。 如果没有与 Number 属性对应的Visual Basic错误,则使用“Application-defined 或 object-defined error”消息。
- HelpFile
- Object
Optional. 可在其中找到有关此错误的帮助的帮助文件的完全限定路径。 如果未指定,则 Visual Basic 使用 Visual Basic 帮助文件的完全限定驱动器、路径和文件名称。
- HelpContext
- Object
Optional. 标识该主题的 HelpFile 上下文 ID 为错误提供了帮助。 如果省略,则使用与 Number 属性对应的错误的Visual Basic帮助文件上下文 ID(如果存在)。
示例
此示例使用 Err 对象的 Raise 方法在Visual Basic编写的函数中生成错误。 调用函数可以捕获错误,并使用消息框将其报告给用户。
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\example\App
' Data\Local\Temporary Projects\ConsoleApplication1\Module1.vb:line 17
' at ConsoleApplication1.Module1.CallingProcedure() in C:\Users\example\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\example\App
' Data\Local\Temporary Projects\ConsoleApplication1\Module1.vb:line 17
' at ConsoleApplication1.Module1.CallingProcedure() in C:\Users\example\AppData
' \Local\Temporary Projects\ConsoleApplication1\Module1.vb:line 25
注解
Raise除可选参数外Number的所有参数。 如果省略可选参数,并且对象的属性设置 Err 包含尚未清除的值,则这些值充当错误的值。
由于该 Err 对象提供的信息比生成语句错误 Error 时更丰富, Raise 因此在编写类模块时生成错误非常有用。 例如,使用 Raise 该方法时,可以在属性中 Source 指定生成错误的源、引用错误的联机帮助等。