views:

57

answers:

3

the following code throws an exception in C++ and catch in C# C++

 throw std::exception ("a C++ exception");

When i catch in C#, it gives me the following:

[SEHException (0x80004005): External component has thrown an exception.]

here is how i call the C++ code

using Foo.Bar.Sample; //C++ library

....

Class1 class1 = new Class1(); //C++ class
class1.throwAnException();

Just wondering how can i get the "a C++ exception" in C#

thanks

+2  A: 

I'd recommend that you always catch exceptions with the same runtime that threw them.

And the only way to analyze the exception is from C++, since the layout of the exception isn't defined.

CodeInChaos
+2  A: 

As @CodeInChaos explained, C++ and .NET have different runtimes so the exceptions do not coexist very well.

Can you use C++/CLI for the C++ part ? This will unify the runtime and the exceptions will be compatible. Note that you can have a small part of the C++ code that uses .NET (mixed-mode code).

Timores
@timores C++/CLI is using in my case. how should i throw my exception properly in C++/CLI thanks
jebberwocky
answering myself...http://en.wikipedia.org/wiki/C%2B%2B/CLI
jebberwocky
+2  A: 
jebberwocky