std::bad_exception - cppreference.com
来自cppreference.com
std::bad_exception 是 C++ 运行时在下列情况抛出的异常类型:
|
(C++11 起) |
|
(C++17 前) |
继承图
|
|
(C++26 起) |
成员函数
注解
| 功能特性测试宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_constexpr_exceptions |
202411L |
(C++26) | constexpr 的异常类型
|
示例
仅可在 C++14 或更早的模式下编译(可能有警告)。
#include <exception> #include <iostream> #include <stdexcept> void my_unexp() { throw; } void test() throw(std::bad_exception) // C++11 摒弃了动态异常说明 { throw std::runtime_error("test"); } int main() { std::set_unexpected(my_unexp); // C++11 中摒弃,C++17 中移除 try { test(); } catch (const std::bad_exception& e) { std::cerr << "捕获到 " << e.what() << '\n'; } }
可能的输出: