开发者

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 cal开发者_如何转开发l 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#


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜