Share via


ErrObject.Number Özellik

Tanım

Hata belirten sayısal bir değer döndürür veya ayarlar. Okuma/yazma.

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

Özellik Değeri

Hata belirten sayısal bir değer döndürür veya ayarlar. Okuma/yazma.

Özel durumlar

Number 65535'ten büyüktür.

Örnekler

Bu örnekte hata işleme yordamında özelliğin Number tipik bir kullanımı gösterilmektedir.

    ' 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

Bu örnek, Visual Basic'te Raise yazılmış bir işlev içinde özgün bir hata oluşturmak için nesnesinin yöntemini kullanırErr. Çağıran işlev hatayı yakalayabilir ve kullanıcıya bildirebilir. Yordamın CallingProcedure bir nesneden türetebileceğiniz bilgi türünü, bir Err nesneden Exception türetebileceğiniz bilgilerle karşıt olduğuna dikkat edin.

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

Açıklamalar

Raise Dışındaki Number tüm bağımsız değişkenler isteğe bağlıdır. İsteğe bağlı bağımsız değişkenleri atlarsanız ve nesnenin Err özellik ayarları temizlenmemiş değerler içeriyorsa, bu değerler hatanızın değerleri olarak görev görür.

Err nesnesi deyimiyle hata oluşturduğunuzdan daha zengin bilgiler verdiğindenError, Raise sınıf modülleri yazarken hata oluşturmak için kullanışlıdır. Örneğin, yöntemiyle Raise , hatayı oluşturan kaynak özelliğinde Source belirtilebilir, hata için çevrimiçi Yardım'a başvurulabilir vb.

Bir nesneden kullanıcı tanımlı bir hata döndürürken, sabite hata kodu VbObjectError olarak seçtiğiniz sayıyı ekleyerek ayarlayınErr.Number. Örneğin, hata kodu olarak 1051 sayısını döndürmek için aşağıdaki kodu kullanırsınız:

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

Şunlara uygulanır

Ayrıca bkz.