[英]C++ Exception not caught by base class
I'm compiling the following code with Visual C++ 2017 (C++17 features are enabled) 我正在使用Visual C ++ 2017编译以下代码(已启用C ++ 17功能)
int main() {
try {
// loot is some library that is linked as a dll
auto game = loot::CreateGameHandle(loot::GameType::fonv, "c:\\something\\invalid", "C:\\something\\invalid");
// throw std::invalid_argument("this works as expected");
}
catch (const std::exception &e) {
std::cout << "caught as exception " << e.what() << std::endl;
}
catch (const std::invalid_argument &e) {
std::cout << "caught as invalid_argument " << e.what() << std::endl;
}
catch (...) {
std::cout << "caught by ..." << std::endl;
}
}
The compiler reports, as expected: 编译器报告,按预期方式:
warning C4286: 'const std::invalid_argument &': is caught by base class ('const stdext::exception &') on line 8
However, the application output is 但是,应用程序输出为
caught as invalid_argument Given game path "c:\something\invalid" does not resolve to a valid directory.
And it's not just changing the catch order or something, if I remove the last 2 catch blocks, the application crashes because of the unhandled exception. 而且,这不仅是更改捕获顺序的方法,还是如果我删除了最后两个捕获块,则由于未处理的异常,应用程序将崩溃。
How is that even possible? 这怎么可能呢? I'm assuming this is somehow related to compiler settings that make my std::exception be a different type from the one std::invalid_argument inside the library inherits from - but why then is my std::invalid_argument the same type as theirs?
我假设这与编译器设置有某种关系,使我的 std :: exception与库中继承的std :: invalid_argument的类型不同-但是为什么我的 std :: invalid_argument与它们的类型相同? Is there a way to fix this?
有没有办法解决这个问题? Because that library throws a lot of different exception types and I can't really catch each one individually.
因为该库引发了许多不同的异常类型,所以我不能真正地单独捕获每个异常。
Bloody hell, sorry for this. 真该死,抱歉。 It turns out the build system I was using added _HAS_EXCEPTIONS=0 to the preprocessor definitions.
事实证明,我正在使用的构建系统将_HAS_EXCEPTIONS = 0添加到了预处理程序定义中。 That's what's causing this.
这就是造成这种情况的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.