Handling errors from .Net components in VB6 code

  • Thread starter Thread starter TJoker .NET
  • Start date Start date
T

TJoker .NET

Hi all.
I'm writing some .net compoents that are also going to be used from vb6
code. Is there a way for me to distinguish all the different types of
exceptions that the .net code my throw ? My .net component can throw a few
different types of exceptions and I need my vb6 code to take action
differently based on which exception it comes accross.
Can I set the error number somehow on my exception classes? (maybe an
attribute ?)

I'd like my vb6 code to look like:

private Sub MySub()
On Error Goto ErrHandling

dim myDotnetObj as DotNetComponent.Class1
set myDotNetObj = new DotNetcomponent.Class1
myDotNetObj.CallMethod()
Exit Sub

ErrHandling:
Select Case err.Number
Case 123: doSomething1
Case 456: doSomething2
End Select

End Sub
 
TJoker .NET,

Check out the section of the .NET framework documentation titled
"HRESULTs and Exceptions", located at (watch for line wrap):

http://msdn.microsoft.com/library/d...n-us/cpguide/html/cpconhresultsexceptions.asp

In it, you will see that certain exceptions map to certain HRESULT error
codes. For your own exceptions, you can override the HResult property to
return an error code for when this exception is marshaled as a COM error
code.

Hope this helps.
 
Thanks, Nicholas. That helped me a lot.
Another question: Do I have to put Guid attributes on my exception classes ?
I got some strange Automation Error errors on my VB6 code before adding the
Guid attributes, but that could have been a coincidence.

Thanks

TJ

Nicholas Paldino said:
TJoker .NET,

Check out the section of the .NET framework documentation titled
"HRESULTs and Exceptions", located at (watch for line wrap):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconhresultsexceptions.asp

In it, you will see that certain exceptions map to certain HRESULT error
codes. For your own exceptions, you can override the HResult property to
return an error code for when this exception is marshaled as a COM error
code.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

TJoker .NET said:
Hi all.
I'm writing some .net compoents that are also going to be used from vb6
code. Is there a way for me to distinguish all the different types of
exceptions that the .net code my throw ? My .net component can throw a few
different types of exceptions and I need my vb6 code to take action
differently based on which exception it comes accross.
Can I set the error number somehow on my exception classes? (maybe an
attribute ?)

I'd like my vb6 code to look like:

private Sub MySub()
On Error Goto ErrHandling

dim myDotnetObj as DotNetComponent.Class1
set myDotNetObj = new DotNetcomponent.Class1
myDotNetObj.CallMethod()
Exit Sub

ErrHandling:
Select Case err.Number
Case 123: doSomething1
Case 456: doSomething2
End Select

End Sub
 
Back
Top