Err.Number and it's relationship to HResult?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TrtnJohn

    Err.Number and it's relationship to HResult?

    I've recently converted some VB 6 code over that used the old non-structured
    exception handling. I also have some .NET classes that raise custom
    exception classes. When the code was VB6 I used to expose my .NET classes
    using COM interop and errors raised from .NET classes were being caught
    correctly in VB6. Now that everything is .NET the old non-structured
    exception handling is still there in the converted classes. But now the
    Err.Number is not reflecting the HResult of the error raised. I wrote a
    simple little Windows Application form with 2 buttons to simulate the
    problem. Here is the code snippet:

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    On Error GoTo ErrHandler
    Err.Description = "Test Error"
    Err.Raise(-2147215379)
    Exit Sub

    ErrHandler:
    MsgBox(Err.Numb er & " : " & Err.Description )
    End Sub

    Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button2.Click
    Try
    Throw New TestException
    Catch ex As Exception
    MsgBox(Err.Numb er & " : " & Err.Description )
    End Try
    End Sub

    Private Class TestException
    Inherits ApplicationExce ption

    Public Sub New()
    MyBase.New()
    MyBase.HResult = -2147215379
    End Sub

    Public Overrides ReadOnly Property Message() As String
    Get
    Return "Test Exception"
    End Get
    End Property
    End Class

    If you click Button1 you get the follwoing message:

    -2147215379 : Test Error

    If you click Button2 you get the following message:

    5 : Test Exception



Working...