133

What are the exception classes that are included in the standard C++ library, and what should they be used for? I know there are a few new C++11 exceptions, but I'm not sure what they are or where they are.

0

2 Answers 2

177
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

Source: http://en.cppreference.com/w/cpp/error/exception
In practice, most exceptions are custom exceptions derived from logic_error and runtime_error. Not that these are neglected, but that many exceptions are domain specific.

Keep in mind that an exception should reflect what went wrong and not who threw it. (No "MyProgramException"s)

8
  • bad_function_call, domain_error, and future_error on msdn they are worst exampled and explained :(
    – Mr.Anubis
    Commented Aug 13, 2012 at 17:08
  • bad_function_call is thrown when you have a default-constructed std::function object and you attempt to call the function that it wraps. Since there is no wrapped function, there's nothing to call. Commented Aug 13, 2012 at 17:12
  • 1
    bad_function_call is thrown when you attempt to invoke std::function that is not ready (aka, default constructed or explicitly cleared via nullptr). future_error is used when you violate one of the many preconditions of the functions for the promise and future. And domain_error is (in theory) for cases where the input to a function is outside the valid range for that function (such as a negative number for std::sqrt).
    – Dave S
    Commented Aug 13, 2012 at 17:13
  • future_error is thrown by various operations on futures when the requested operation is invalid or would put the object into an invalid state. This is new stuff in C++11, and I can't fit a tutorial in a comment. Commented Aug 13, 2012 at 17:13
  • 4
    cppreference lists the derived classes of std::exception, and notes whether they are C++11 (in particular, std::ios_base::failure moved from std::exception to std::system_error). Usage and header are one link away.
    – ecatmur
    Commented Aug 13, 2012 at 17:54
56

See this site

enter image description here

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.
5
  • 1
    This is good, but is missing the C++11 exceptions, and doesn't show which exceptions are in which headers. Commented Aug 13, 2012 at 17:38
  • 3
    @MooingDuck Your question was tagged c++, not c++11, and they all reside in the same <stdexcept> Commented Aug 13, 2012 at 18:00
  • 13
    C++ means whatever the latest version is, while C++11 and C++03 are questions about those specific versions. my question isn't about a specific version, just the most up-to-date info on C++. Either way, I'll edit the question to mention C++11. Also, not all of those errors are in <stdexcept> as shown by ideone.com/uqM6h Commented Aug 13, 2012 at 18:03
  • 2
    @MooingDuck If not specifically asked, then an answer for C++ 03 is as valid as one for C++ 11 and vice versa. It was your responsability to provide all necessary informations. You should never expect to get get quality answers from poor question. Period.
    – Phil1970
    Commented May 12, 2017 at 3:39
  • std::logic_error, not std::logic_failure. That diagram is wrong!
    – Galaxy
    Commented Oct 29, 2018 at 0:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.