ErrObject.Number 属性

定义

返回或设置指定错误的数值。 读/写。

public:
 property int Number { int get(); void set(int value); };
public int Number { get; set; }
member this.Number : int with get, set
Public Property Number As Integer

属性值

返回或设置指定错误的数值。 读/写。

例外

Number 大于 65535。

示例

此示例演示了 在错误处理例程中属性 Number 的典型用法。

    ' Typical use of Number property.
Sub test()
  On Error GoTo out

  Dim x, y As Integer
  x = 1 / y   ' Create division by zero error.
  Exit Sub
out:
  MsgBox(Err.Number)
  MsgBox(Err.Description)
  ' Check for division by zero error.
  If Err.Number = 11 Then
      y = y + 1
  End If
  Resume Next
End Sub

此示例使用 Err 对象的 Raise 方法在用 Visual Basic 编写的函数中生成原始错误。 调用函数可以捕获错误并将其报告给用户。 请注意,过程 CallingProcedure 会将可从 对象派生 Err 的信息类型与可从 对象派生 Exception 的信息进行对比。

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

注解

除 之外Number的所有Raise参数都是可选的。 如果省略可选参数,并且对象的属性设置 Err 包含尚未清除的值,则这些值将用作错误的值。

由于 对象 Err 提供的信息比使用 语句生成错误 Error 时更丰富, Raise 因此在编写类模块时生成错误非常有用。 例如,使用 Raise 方法,可以在 属性中 Source 指定生成错误的源,可以引用错误的联机帮助,等等。

从 对象返回用户定义的错误时,通过将所选数字作为错误代码添加到VbObjectError常量进行设置Err.Number。 例如,使用以下代码将数字 1051 作为错误代码返回:

Err.Raise(Number:=vbObjectError + 1051, Source:="SomeClass")

适用于

另请参阅