Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark Traudt - 23 Jul 2004 03:07 GMT
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.
Ronald Laeremans [MSFT] - 29 Jul 2004 00:20 GMT
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

> The following managed C++ function is called from an unmanaged C++ DLL:
>
[quoted text clipped - 26 lines]
> compiled with the /clr:initialAppDomain flag, because I need to make sure
> unmanaged calls are executed in this domain.
Vinayak Raghuvamshi - 29 Jul 2004 16:45 GMT
> The following managed C++ function is called from an unmanaged C++ DLL:  
>
[quoted text clipped - 6 lines]
>
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.