【问题标题】:What exception classes are in the standard C++ library标准 C++ 库中有哪些异常类
【发布时间】:2012-08-09 23:00:01
【问题描述】:

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

【问题讨论】:

    标签: c++ exception c++11 stl


    【解决方案1】:
    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 派生的自定义异常。并不是这些被忽略,而是许多例外是特定领域的。

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

    【讨论】:

    • bad_function_call, domain_error, and future_error 在 msdn 上他们是最糟糕的例子和解释:(
    • bad_function_call 当你有一个默认构造的 std::function 对象并且你试图调用它包装的函数时抛出。由于没有包装函数,所以没有什么可调用的。
    • bad_function_call 当您尝试调用未准备好的 std::function 时抛出(又名,默认构造或通过 nullptr 显式清除)。当您违反promisefuture 函数的许多前提条件之一时,使用future_error。而domain_error(理论上)适用于函数输入超出该函数有效范围的情况(例如std::sqrt 的负数)。
    • future_error 在请求的操作无效或将对象置于无效状态时由期货上的各种操作抛出。这是 C++11 中的新内容,我无法在评论中添加教程。
    • cppreference 列出了std::exception 的派生类,并注明它们是否为C++11(特别是std::ios_base::failurestd::exception 移动到std::system_error)。用法和标题是一个链接。
    【解决方案2】:

    看到这个site

    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 异常,并且没有显示哪些异常在哪些标头中。
    • @MooingDuck 你的问题被标记为c++,而不是c++11,它们都位于同一个&lt;stdexcept&gt;
    • C++ means whatever the latest version is, while C++11 and C++03 are questions about those specific versions。我的问题不是关于特定版本,而是关于 C++ 的最新信息。无论哪种方式,我都会编辑问题以提及 C++11。此外,并非所有这些错误都在&lt;stdexcept&gt; 中,如ideone.com/uqM6h 所示
    • @MooingDuck 如果没有特别要求,那么 C++ 03 的答案与 C++ 11 的答案一样有效,反之亦然。您有责任提供所有必要的信息。你永远不应该期望从糟糕的问题中得到高质量的答案。期间。
    • std::logic_error,而不是 std::logic_failure。那张图是错的!
    猜你喜欢
    • 2023-03-25
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多