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


102

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

Answers:


124
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_error和派生的自定义异常runtime_error。并不是说这些都是被忽略的,而是许多例外是特定于领域的。

请记住,异常应该反映出了什么问题而不是由谁抛出的。(无“ MyProgramException”)


bad_function_call, domain_error, and future_error在msdn上,它们是最坏的例子,并得到了解释:(
Mr.Anubis

bad_function_call当您有一个默认构造的std :: function对象并尝试调用它包装的函数时,将抛出该异常。由于没有包装函数,因此没有什么可调用的。
皮特·贝克尔

1
bad_function_call当您尝试调用std::function尚未准备就绪(也就是默认构造或通过nullptr明确清除)的调用时抛出该异常。 future_error当您违反promise和功能的多个前提之一时,将使用future。和domain_error是(理论上)为例,其中输入到功能是外该功能的有效范围(如对于一个负数std::sqrt)。
Dave S

future_error当所请求的操作无效或将对象置于无效状态时,期货上的各种操作会抛出该异常。这是C ++ 11中的新内容,我无法在注释中添加教程。
皮特·贝克尔

3
cppreference列出了的派生类std::exception,并指出它们是否为C ++ 11(尤其是std::ios_base::failurestd::exception移至std::system_error)。用法和标头是一个链接。
ecatmur

50

看这个网站

在此处输入图片说明

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.

很好,但是缺少C ++ 11异常,并且没有显示哪个标头包含哪些异常。
Mooing Duck 2012年

3
@MooingDuck您的问题被标记了c++,不是c++11,并且它们都位于同一位置<stdexcept>
TemplateRex

6
C ++表示最新版本,而C ++ 11和C ++ 03则是有关那些特定版本的问题。我的问题不是关于特定版本的,而是关于C ++的最新信息。无论哪种方式,我都将编辑问题以提及C ++ 11。另外,并非所有这些错误都<stdexcept>ideone.com/uqM6h
Mooing Duck

1
@MooingDuck如果没有特别要求,则C ++ 03的答案与C ++ 11的答案一样有效,反之亦然。提供所有必要的信息是您的责任。您永远都不要期望从糟糕的问题中获得高质量的答案。期。
Phil1970年

std::logic_error不是std::logic_failure。该图是错误的!
银河
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.