Ale*_*sev 13 c++ macos std rtti c++14
今天我在 C++ 中捕获异常时遇到了一个奇怪的行为,有人可以向我解释一下吗?代码片段
#include <iostream>
#include <string>
#include <exception>
int main() {
try {
std::stod("notanumber");
} catch (const std::invalid_argument&) {
std::cerr << "std::invalid_argument" << std::endl;
} catch (const std::out_of_range&) {
std::cerr << "std::out_of_range" << std::endl;
} catch (const std::exception&) {
std::cerr << "Caught by ancestor" << std::endl;
} catch (...) {
auto ptr = std::current_exception();
auto type = __cxxabiv1::__cxa_current_exception_type();
std::cerr << type->name() << std::endl;
std::cerr << "..." << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
写入输出
St16invalid_argument
...
Run Code Online (Sandbox Code Playgroud)
环境详情
C++ 14, disabled RTTI
Clang 13.1.6 arm64-apple-darwin-21.6.0
macOS Monterey 12.6
Run Code Online (Sandbox Code Playgroud)
我希望在第一个 catch 块上捕获异常
更新。即使是最简单的捕获对我来说对环境也不起作用
try {
std::stod("notanumber");
} catch (const std::invalid_argument&) { // not caught
std::cerr << "std::invalid_argument" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
378 次 |
| 最近记录: |