为什么我的C ++异常没有被捕获? [英] Why are my C++ exceptions not being caught?
问题描述
我有一些使用非常标准的异常模式的C ++代码:
I have some C++ code that uses a very standard exception pattern:
try {
// some code that throws a std::exception
}
catch (std::exception &e) {
// handle the exception
}
问题是异常没有被捕获,我不知道为什么。
The problem is that the exceptions are not being caught and I cannot figure out why.
代码在OS X中编译为静态库(通过Xcode)。该库链接到一个Cocoa应用程序,通过Objective-C ++ thunk调用所涉及的函数。我怀疑Objective-C和C ++之间的相互作用是罪魁祸首,但我的所有尝试固定下来失败了。
The code compiles to a static library in OS X (via Xcode). The library is linked into a Cocoa application, with a call to the function in question happening via an Objective-C++ thunk. I suspect that the interplay between Objective-C and C++ is the culprit but all my attempts to pin this down have failed.
我没有能够创建一个简单的例子在一个简单的例子中再现这种行为。当我从大的程序的上下文中取出相关的代码一切正常。
I have not been able to create a simple example that reproduces this behavior in a simple example. When I take the relevant code out of the context of my big program everything works.
任何人都可以建议为什么我的异常没有被捕获?
Can anyone suggest why my exceptions are not being caught?
推荐答案
感谢大家的意见。这些是对任何遇到类似问题的人的好建议。它现在正在工作,但我不能100%确定我做出的各种更改中的哪一个导致事情再次变得健全。
Thanks for the input from everyone. Those are good suggestions for anyone who runs into a similar problem. It's working now, but I'm not 100% sure which of various changes I made caused things to become sane again. Once again, the approach of simplifying down to something that works and building back up from there paid off.
在回复中没有提到的一件事情,我在回答中提到了一件事认为是我的困惑的一部分,是确保处理程序明确,它实际上捕获了异常。我认为在我的处理程序的一些配方中掩盖了这个事实,并将异常传递到更高级别的处理程序。
One thing that wasn't mentioned in the responses, and which I think was part of my confusion, is to make sure that the handler makes it obvious that it actually caught the exception. I think that in some of my formulations of the handler it was masking that fact and passing the exception on to a higher level handler.
这篇关于为什么我的C ++异常没有被捕获?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!