What exception classes are in the standard C++ library

后端 未结 2 1474
心在旅途
心在旅途 2020-12-12 10:06

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 su

2条回答
  •  -上瘾入骨i
    2020-12-12 11:06

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

提交回复
热议问题