Exception thrown in managed C++ not understood in unmanaged C++

G

Guest

The following managed C++ function is called from an unmanaged C++ DLL:

double Divide( double num, double denom )
{
if( denom == 0.0 )
throw new std::exception( "divide by zero" );
return num / denom;
}

The caller calls the function within a C++ try-catch block, with the following two catch blocks:

catch( std::exception& ex ) { // do something }
catch( ... ) { // do something else }

If my managed code is running in the DefaultDomain and throws the exception, then the first catch block is executed. However, if the code is running in another appdomain, then the second catch block is executed!

I cannot find anything documented on MSDN or the Web that explains this behavior. I also have not found any workaround (I can use SEH primitives directly in my unmanaged code to catch or "translate" the exception, but what I really want is access to the exception message).

I am running Visual Studio .NET 2003 on Windows XP.

My managed assembly is loaded first into the non-default domain (another assembly loads it manually prior to any calls from unmanaged code). It is compiled with the /clr:initialAppDomain flag, because I need to make sure unmanaged calls are executed in this domain.
 
R

Ronald Laeremans [MSFT]

Hi Mark,

We weren't able to reproduce this problem. Can you post or mail me (obvious
transformation of my posting alias gives my real email address) a full repro
case?

Thanks.

Ronald
 
V

Vinayak Raghuvamshi

Mark Traudt said:
The following managed C++ function is called from an unmanaged C++ DLL:

double Divide( double num, double denom )
{
if( denom == 0.0 )
throw new std::exception( "divide by zero" );
return num / denom;
}

The caller calls the function within a C++ try-catch block, with the following two catch blocks:

aside from your stated problem, I notice something wierd. you are
doing a throw new std::exception("divide by zero") and then catching
it as catch( std::exception& ex );

apart from a memory leak, i dont see how your catch statement is
getting invoked at all....

you really dont need to do a new for your exception as long as you are
catching as a const ref...

-Vinayak
 

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