共用方式為


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

適用於

另請參閱