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

Moo*_*uck 96 c++ stl exception c++11

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

Moo*_*uck 116

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
Run Code Online (Sandbox Code Playgroud)

来源:http://en.cppreference.com/w/cpp/error/exception
实际上,大多数例外是从logic_error和派生的自定义异常runtime_error.并非这些都被忽略了,但许多例外都是针对特定领域的.

请记住,异常应该反映出错了,而不是谁扔了它.(没有"MyProgramException")

  • cppreference列出[`std :: exception`]的派生类(http://en.cppreference.com/w/cpp/error/exception),并注意它们是否是C++ 11(特别是`std: :ios_base :: failure`从`std :: exception`移到`std :: system_error`).用法和标题是一个链接. (3认同)

Tem*_*Rex 49

看到这个网站

在此输入图像描述

Exception               Description
===================================
std::exception          An exception and parent class of all the standard C++ exceptions.
std::bad_alloc          This can be thrown by new.
std::bad_cast           This can be thrown by dynamic_cast.
std::bad_exception      This is useful device to handle unexpected exceptions in a C++ program
std::bad_typeid         This can be thrown by typeid.
std::logic_error        An exception that theoretically can be detected by reading the code.
std::domain_error       This is an exception thrown when a mathematically invalid domain is used
std::invalid_argument   This is thrown due to invalid arguments.
std::length_error       This is thrown when a too big std::string is created
std::out_of_range       This can be thrown by the at method from for example a std::vector and std::bitset<>::operator[]().
std::runtime_error      An exception that theoretically can not be detected by reading the code.
std::overflow_error     This is thrown if a mathematical overflow occurs.
std::range_error        This is occured when you try to store a value which is out of range.
std::underflow_error    This is thrown if a mathematical underflow occurs.
Run Code Online (Sandbox Code Playgroud)

  • [C++表示最新版本,而C++ 11和C++ 03是关于这些特定版本的问题](http://meta.stackexchange.com/questions/112641/when-did-the-c-tag -start到暗示-C11按默认值).我的问题不是关于特定版本,而是关于C++的最新信息.无论哪种方式,我都会编辑提问C++ 11的问题.此外,并非所有这些错误都在`<stdexcept>`中,如http://ideone.com/uqM6h所示 (6认同)
  • @MooingDuck你的问题被标记为`c ++`,而不是`c ++ 11`,它们都存在于同一个`<stdexcept>中 (3认同)
  • @MooingDuck 如果没有特别要求,那么 C++ 03 的答案与 C++ 11 的答案一样有效,反之亦然。您有责任提供所有必要的信息。你永远不应该期望从糟糕的问题中得到高质量的答案。时期。 (2认同)