std::bad_exception_C++中文网

std::bad_exception 是 C++ 运行时在下列情况抛出的异常类型:

2)动态异常规定被违背且 std::unexpected 抛出或重抛仍然违背异常规定的异常,但异常规定允许 std::bad_exception ,则抛出 std::bad_exception

(C++17 前)
cpp/error/exception

std-bad exception-inheritance.svg

继承图

成员函数

继承自 std::exception

成员函数

析构该异常对象
(std::exception 的虚公开成员函数)
返回解释性字符串
(std::exception 的虚公开成员函数)

示例

#include <iostream>
#include <exception>
#include <stdexcept>
 
void my_unexp() { throw; }
 
void test() throw(std::bad_exception)
{
    throw std::runtime_error("test");
}
 
int main()
{
    std::set_unexpected(my_unexp);
    try {
         test();
    } catch(const std::bad_exception& e)
    {
        std::cerr << "Caught " << e.what() << '\n';
    }
}

输出:

Caught std::bad_exception