Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Readable C++\CLI Exception Message in C#?

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#

like image 893
jebberwocky Avatar asked Oct 15 '10 09:10

jebberwocky


3 Answers

Didn't ask the question correctly. It is C++\CLI not C++ exception. In C++\CLI, do the following:

 throw gcnew System::Exception("It is a C++\CLI exception");

rather than c++ native exception

thank you all for answering and commenting

like image 136
jebberwocky Avatar answered Oct 07 '22 20:10

jebberwocky


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.

like image 36
CodesInChaos Avatar answered Oct 07 '22 20:10

CodesInChaos


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).

like image 44
Timores Avatar answered Oct 07 '22 20:10

Timores