Handling errors from .Net components in VB6 code

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
T

TJoker .NET

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top