开发者

C++ Standard Library Exception List?

开发者 https://www.devze.com 2022-12-31 00:17 出处:网络
Is there a reference about C++ Standard Library Exceptions? I just want to know that which functions may throw an exception or n开发者_开发问答ot.Actually, most of the standard library function don\'t

Is there a reference about C++ Standard Library Exceptions? I just want to know that which functions may throw an exception or n开发者_开发问答ot.


Actually, most of the standard library function don't throw exceptions themselves. They just pass on exception thrown by user code invoked by them. For example, if you push_back() an element to a vector, this can throw (due to memory allocation errors and) if the object's copy constructor throws.

A few notable exceptions (no pun intended) where library functions throw are:

  • Some methods will throw out_of_range if the index provided is invalid:
    • std::vector<>::at()
    • std::basic_string<>::at()
    • std::bitset<>::set(), reset() and flip().
  • Some methods will throw std::overflow_error on integer overflow:
    • std::bitset<>::to_ulong() and (C++0x) to_ullong().
  • std::allocator<T> will pass on std::bad_alloc thrown by new which it invokes.
  • Streams can be setup so that std::ios_base::failure are thrown when a state bit is set.
  • Large array allocations can throw std::bad_array_new_length
  • dynamic_cast on a reference can throw a std::bad_cast (technically not part of the standard library)
  • Throwing an invalid exception from a function with an exception specification will throw a std::bad_exception
  • Calling a std::function::operator(...) if it has no value will throw std::bad_function_call.
  • Using typeinfo of a null pointer may throw a std::bad_typeid.
  • Accessing a weak_ptr after the pointee has been released will throw a std::bad_weak_ptr.
  • Incorrect usage of std::promise/std::future may throw a std::future_error.
  • (c++11) The string conversion functions std::stoi, std::stol, std::stoll, std::stoul, std::stoull, std::stof, std::stod, and std::stold can throw both std::invalid_argument and std::out_of_range.
  • (c++11) In the regex family, constructors and assign methods can throw std::regex_error.

(I'm making this a CW answer, so if anyone can think of more such, please feel free to append them here.)

Also, for the 3rd edition of The C++ Programming Language, Bjarne Stroustrup has a downloadable appendix about exception safety, which might be relevant.


The only functions guaranteed (by the compiler) to not throw are functions that have the throw() exception specification, like this:

void ThisFunctionNeverThrows() throw()
{
}

Otherwise, any other function can potentially throw an exception, unless they are specifically documented otherwise. You must consider exception safety when writing code in the face of exceptions.

See Bjarne Stroustup's article on exception safety and the standard library: http://www2.research.att.com/~bs/3rd_safe.pdf Starting on page 19 in the PDF you can find information on guarantees made by the standard containers.

0

精彩评论

暂无评论...
验证码 换一张
取 消