标准 C++ 库中有哪些异常类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11938979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What exception classes are in the standard C++ library
提问by Mooing Duck
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.
标准 C++ 库中包含哪些异常类,它们应该用于什么?我知道有一些新的 C++11 例外,但我不确定它们是什么或它们在哪里。
回答by Mooing Duck
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.
来源:http: //en.cppreference.com/w/cpp/error/exception
实际上,大多数异常是从logic_error
和派生的自定义异常runtime_error
。并不是说这些被忽略了,而是许多例外是特定于领域的。
Keep in mind that an exception should reflect what went wrong and notwho threw it. (No "MyProgramException"s)
请记住,异常应该反映出了什么问题,而不是是谁抛出的。(没有“MyProgramException”)
回答by TemplateRex
See this 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.