在C ++ / CLI的包装类转换异常的最佳实践 [英] Best practice for translating exceptions in C++/CLI wrapper class

查看:216
本文介绍了在C ++ / CLI的包装类转换异常的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个.NET包装类而引发异常现有的原生类。什么是原生的C ++异常和托管异常之间进行转换的最佳做法? ( - > System.System.ArgumentException如性病:: invalid_argument)上一到一的基础上捕捉并重新抛出?有没有已经制定了地方?映射

I am writing a .NET wrapper class for an existing native class which throws exceptions. What are the best practices for translating between native C++ exceptions and Managed exceptions? Catch and re-throw on a one-to-one basis (e.g. std::invalid_argument -> System.System.ArgumentException)? Is there a mapping already drawn up somewhere?

推荐答案

还有,我知道没有标准的映射。我已经在过去做的是翻译的人我了解,和System.Runtime.InteropServices.SEHException catch块。所有非翻译的异常将被转换成了例外。只要您拥有的code被抛出异常调试版本,你应该得到一个不错的堆栈跟踪。然后,你可以去看看例外,写的包装。

There is no standard mapping that I know of. What I've done in the past is translate the ones I know about, and a catch block for System.Runtime.InteropServices.SEHException. All non-translated exceptions will be turned into that exception. As long as you have a debug build of the code that is throwing the exception, you should get a nice stack trace. Then you can go and look at the exception and write the wrapper.

但在最后一个项目,我不得不这样做,我去的东西更简单,我最后写了几个System.Exception的衍生物用于logic_error和runtime_error。然后,我会赶上那些2基类和使用typeid的(错误)编写得到了抛出的.NET消息。这样,我也没有输什么正在从C ++抛出,但没有映射以外的所有最重要的。

But on the last project I had to do this on, I went with something much more simple, I ended up writing a couple of System.Exception derivatives for logic_error and runtime_error. Then I would catch those 2 base classes and use typeid(err) to write the .NET message that got thrown. This way I didn't "lose" what was being thrown from the C++ but didn't have to map everything except the most important ones.

这篇关于在C ++ / CLI的包装类转换异常的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆