Unit Tests and COM Exceptions

B

batvanio

Hi,

Sorry I am bringing this topic again, but something does not feel
right:
I am trying to use VS 2005 unit test to prove that a COM component that
I use is throwing the right COM exception. So I am trying this

try
{
COMComponent.GenerateCOMException(); // this throws the COM
exception
}
catch (System.Runtime.InteropServices.COMException e)
{
// this should show the HRESULT of the COM exception....
MessageBox.Show(e.ErrorCode.ToString());
}

However, as soon as the exception is generated, the unit test fails and
does non even go to the catch statement - exits immediately. Funny
thing is that if I use a generic exception like catch (System.Exception
e), it DOES go to the catch statement. However, the generic exception
does not give me the ErrorCode, which is a member of COMException only.


If i try the ExpectedException attribute, as soon as it generates the
exception, it exits, again without reaching the catch statement, this
time with Passed result...

Does anyone have any idea what is going on....?

Thanks,

Ivan
 
M

Mattias Sjögren

However, as soon as the exception is generated, the unit test fails and
does non even go to the catch statement - exits immediately. Funny
thing is that if I use a generic exception like catch (System.Exception
e), it DOES go to the catch statement. However, the generic exception
does not give me the ErrorCode, which is a member of COMException only.


If i try the ExpectedException attribute, as soon as it generates the
exception, it exits, again without reaching the catch statement, this
time with Passed result...

Does anyone have any idea what is going on....?


Some HRESULTs are mapped to specific .NET exception types and not to
the generic COMException. For those you can use
Marshal.GetHRForException().


Mattias
 
G

Guest

I believe that Win32Exception has some very helpful methods. Check into the
docs for it.
 

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