处理 C++CLI 中的异常
在非托管 C++ 中,当前的想法是始终通过 const 引用捕获异常。
我对托管 C++ 没有那么丰富的经验(尽管我正在处理这些差异),但我想知道,在这里捕获异常的最佳方法是什么?句柄会捕获异常吗 ie
try
{
}
catch( ExceptionType^ ex )
{
}
.. 如果是的话,会有任何警告吗?
In unmanaged C++ the current thinking is to always catch exceptions via const reference.
I'm not that experienced with managed C++ (although I'm getting to grips with the differences) but I'm wondering, what is the best way to catch exceptions here? Would exceptions ever be caught by handle i.e.
try
{
}
catch( ExceptionType^ ex )
{
}
.. and if so, would there be any caveats?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于处理托管代码异常,是的,这就是这样做的方法。
但是,如果您使用托管 C++,我想这是因为您需要与本机代码进行互操作(否则您只是一个受虐狂?抱歉,我不喜欢托管 C++),在这种情况下事情会更加复杂。请参阅 此处了解有关混合模式异常处理问题的一些背景信息。
For handling managed code exceptions, yes that's the way to do it.
However if you are using Managed C++, I imagine this is becase you have the need to interop with native code (or else you are just a masochist? sorry, I am no fan of Managed C++), in which case things are more complicated. See here for some background on mixed-mode exception handling issues.
当抛出托管异常时,除了通过垃圾收集句柄之外,没有其他方法可以捕获它。与 C++ 不同,托管代码对于可以抛出什么和不能抛出什么有更严格的规则。
When you throw a managed exception, then there is no way to catch it except by garbage-collected handle. Managed code has much stricter rules about what can and cannot be thrown, unlike C++.