C++ 参考手册

位置:首页 > C++ 参考手册 >C++ 标准库头文件 > 标准库头文件 <exception>

此头文件是错误处理库的一部分。

标准库组件抛出的各种异常的基类
(类)
用于捕获并存储当前异常的混入类型
(类)
std::current_exception 无法复制异常对象时抛出的异常
(类)

typedef

(C++17 中移除)
std::unexpected 所调用的函数类型
(typedef)
std::terminate 所调用的函数类型
(typedef)
用于处理异常对象的共享指针类型
(typedef)

函数

(C++17 中移除)
违背动态异常说明时调用的函数
(函数)
检查当前是否正在进行异常处理
(函数)
从异常对象创建一个std::exception_ptr
(函数模板)
捕获当前异常到 std::exception_ptr 之中
(函数)
从一个 std::exception_ptr 抛出异常
(函数)
抛出实参,带上混入的 std::nested_exception
(函数模板)
抛出 std::nested_exception 中的异常
(函数模板)
异常处理失败时调用的函数
(函数)
获得当前的 terminate_handler
(函数)
更改将被 std::terminate 调用的函数
(函数)
(C++11)(C++17 中移除)
获得当前的 unexpected_handler
(函数)
(C++17 中移除)
更改要被 std::unexpected 调用的函数
(函数)

概要

namespace std {
    class exception;
    class bad_exception;
    class nested_exception;
 
    typedef void (*unexpected_handler)();
    unexpected_handler get_unexpected() noexcept;
    unexpected_handler set_unexpected(unexpected_handler f) noexcept;
    [[noreturn]] void unexpected();
 
    typedef void (*terminate_handler)();
    terminate_handler get_terminate() noexcept;
    terminate_handler set_terminate(terminate_handler f) noexcept;
    [[noreturn]] void terminate() noexcept;
 
    bool uncaught_exception() noexcept;
 
    typedef /* 未指明 */ exception_ptr;
 
    exception_ptr current_exception() noexcept;
    [[noreturn]] void rethrow_exception(exception_ptr p);
    template<class E> exception_ptr make_exception_ptr(E e) noexcept;
 
    [[noreturn]] template <class T> void throw_with_nested(T&& t);
    template <class E> void rethrow_if_nested(const E& e);
}

std::exception

class exception {
public:
    exception() noexcept;
    exception(const exception&) noexcept;
    exception& operator=(const exception&) noexcept;
    virtual ~exception();
    virtual const char* what() const noexcept;
};

std::bad_exception

class bad_exception : public exception {
public:
    bad_exception() noexcept;
    bad_exception(const bad_exception&) noexcept;
    bad_exception& operator=(const bad_exception&) noexcept;
    virtual const char* what() const noexcept;
};


std::nested_exception

class nested_exception {
public:
    nested_exception() noexcept;
    nested_exception(const nested_exception&) noexcept = default;
    nested_exception& operator=(const nested_exception&) noexcept = default;
    virtual ~nested_exception() = default;
 
    // 访问函数
    [[noreturn]] void rethrow_nested() const;
    exception_ptr nested_ptr() const noexcept;
};

参阅