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:
If you click Button1 you get the following message:
-2147215379 : Test Error
If you click Button2 you get the following message:
5 : Test Exception
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
On Error GoTo ErrHandler
Err.Description = "Test Error"
Err.Raise(-2147215379)
Exit Sub
ErrHandler:
MsgBox(Err.Number & " : " & Err.Description)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Throw New TestException
Catch ex As Exception
MsgBox(Err.Number & " : " & Err.Description)
End Try
End Sub
Private Class TestException
Inherits ApplicationException
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 following message:
-2147215379 : Test Error
If you click Button2 you get the following message:
5 : Test Exception