473,395 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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.

Nov 17 '05 #1
2 1981
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

"Mark Traudt" <Ma********@discussions.microsoft.com> wrote in message
news:22**********************************@microsof t.com...
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.

Nov 17 '05 #2
"Mark Traudt" <Ma********@discussions.microsoft.com> wrote in message news:<22**********************************@microso ft.com>...
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
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bret Pehrson | last post by:
Suppose the following: // Unmanaged code class UnmanagedException /* not visible outside of unmanaged code */ { }; void DoSomething() /* visible (exported) to managed code */ { throw new...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
0
by: Joe | last post by:
Reposting here as there were no useful replies in the dotnet.framework NG... What is the correct pattern for handling exceptions in IDisposable.Dispose, especially in a class that manages...
3
by: Tommy Svensson \(InfoGrafix\) | last post by:
I've been instructed to work againt a huge unmanaged C++ API from a C# application. Now, the only way, as I've understood it, is to go the Managed Extensions for C++ way. This means I have to...
3
by: Thorsten | last post by:
HI I'm a C# developer and unfortunately I have to write now some code in managed and unmanaged C++. In this area I'm Newbie and therefore please forgive me if this is a really simple...
7
by: Søren Dreijer | last post by:
Hi, I have a mixed C#, managed C++ and unmanaged C++ project. The managed class calls a method which exists in an unmanaged singleton class. During the entire lifetime of the application, this...
11
by: Don | last post by:
When using Visual Basic .NET with a reference to Interop.Outlook, is there a way to get more detailed information about an error other than Exception.Message or Exception.ToString? For example,...
2
by: Maxim | last post by:
Hi, All! How do I get unmanaged exception type in the catch block. Here's the piece of code to clarify my question. try { throw int(1); } catch(Exception^ ex)
3
by: osudude | last post by:
What happens in the following scenario? 1.Via a DLL interface, a managed thread kicks off a potentially long running FOR loop operation encapsulated by a native C++ dll. This FOR loop has C++...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.