温馨提示×

C++标准异常类的继承关系

c++
小樊
82
2024-11-08 22:54:26
栏目: 编程语言

C++ 标准异常类位于 <exception> 头文件中,它们继承自 std::exception 类。std::exception 是一个基类,它为所有标准异常提供了一个公共的接口。以下是 C++ 标准异常类的继承关系:

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

class std::runtime_error : public std::exception {
public:
    explicit runtime_error(const std::string& message) noexcept;
    const char* what() const noexcept override;
};

class std::out_of_range : public std::runtime_error {
public:
    explicit out_of_range(const std::string& message) noexcept;
    const char* what() const noexcept override;
};

class std::bad_alloc : public std::runtime_error {
public:
    explicit bad_alloc() noexcept;
    const char* what() const noexcept override;
};

class std::logic_error : public std::exception {
public:
    explicit logic_error(const std::string& message) noexcept;
    const char* what() const noexcept override;
};

class std::domain_error : public std::runtime_error {
public:
    explicit domain_error(const std::string& message) noexcept;
    const char* what() const noexcept override;
};

class std::invalid_argument : public std::runtime_error {
public:
    explicit invalid_argument(const std::string& message) noexcept;
    const char* what() const noexcept override;
};

class std::length_error : public std::runtime_error {
public:
    explicit length_error(const std::string& message) noexcept;
    const char* what() const noexcept override;
};

class std::out_of_range : public std::runtime_error {
public:
    explicit out_of_range(const std::string& message) noexcept;
    const char* what() const noexcept override;
};

这里列出了部分 C++ 标准异常类,它们都是从 std::exception 类继承而来的。每个异常类都有其特定的用途和构造函数,以便在程序中抛出和捕获相应的异常。

0