c++ - 标准 C++ 库中有哪些异常类

标签 c++ exception c++11 stl

标准 C++ 库中包含哪些异常类,它们的用途是什么?我知道有一些新的 C++11 异常,但我不确定它们是什么或它们在哪里。

最佳答案

std::exception <exception> interface (debatable if you should catch this)
    std::bad_alloc <new> failure to allocate storage
        std::bad_array_new_length <new> invalid array length
    std::bad_cast <typeinfo> execution of an invalid dynamic-cast
    std::bad_exception <exception> signifies an incorrect exception was thrown
    std::bad_function_call <functional> thrown by "null" std::function
    std::bad_typeid <typeinfo> using typeinfo on a null pointer
    std::bad_weak_ptr <memory> constructing a shared_ptr from a bad weak_ptr
    std::logic_error <stdexcept> errors detectable before the program executes
        std::domain_error <stdexcept> parameter outside the valid range
        std::future_error <future> violated a std::promise/std::future condition
        std::invalid_argument <stdexcept> invalid argument
        std::length_error <stdexcept> length exceeds its maximum allowable size
        std::out_of_range <stdexcept> argument value not in its expected range
    std::runtime_error <stdexcept> errors detectable when the program executes
        std::overflow_error <stdexcept> arithmetic overflow error.
        std::underflow_error <stdexcept> arithmetic underflow error.
        std::range_error <stdexcept> range errors in internal computations
        std::regex_error <regex> errors from the regular expression library.
        std::system_error <system_error> from operating system or other C API
            std::ios_base::failure <ios> Input or output error

来源:http://en.cppreference.com/w/cpp/error/exception
在实践中,大多数异常是从 logic_errorruntime_error 派生的自定义异常。并不是这些被忽略,而是许多异常(exception)是特定领域的。

请记住,异常应该反射(reflect)出了什么问题,不是是谁抛出的。 (没有“MyProgramException”)

关于c++ - 标准 C++ 库中有哪些异常类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11938979/

相关文章:

python - 作为参数的嵌套模板函数

c++ - 结合使用 FreeGlut 和 SDL

powershell - 如何从 PowerShell 中的 catch block 重新抛出异常?

java - 从变量引发异常

c++ - 是否存在将 push_back 替换为 emplace_back 不正确的情况?

c++ - unique_ptr 成员的 vector

c++ - 动态 vector 类容器,但其元素保存其索引?

c++ - 全部继承自同一类的对象映射,调用对象方法而不是父类;由 小码哥发布于

c# - 类型 'System.String[]' 不支持比较运算符

c++ - 递归可变参数函数模板的返回类型的decltype