I know the STL will throw on a memory allocation error or if the contained type throws in its constructor / assignment operator.
Otherwise, apparently 'a few' STL methods can throw other exceptions. The example everyone seems to mention is vector::at(), but I can't find a list of the others anywhere.
Does anyone know of such a list? 开发者_开发问答
Won't be 100% accurate, and is for C++03, but a half-hour effort based on grepping through GCC 4.3.4 includes, ignoring tr1 and ext but including iostream. Crucially, some of these checks might be due to this implementation prefering more defensive coding, and might not be mandated in the Standard and available universally....
bitset
std::overflow_error
-.to_ulong()
when too many bits to fit in unsigned longstd::out_of_range
-operator[]()
attempt past end
new
std::bad_alloc
typeinfo
std::bad_cast
on invaliddynamic_cast
attempt
ios
std::ios_base::failure
when using exception masks for error reporting
string
out_of_range
-at
/append
/assign
/insert
/erase
/replace
/copy
/substr
length_error
: attempt to exceedmax_size()
duringreserve
or implicit resize (e.g.assign
/insert
/+=
etc.)
locale
std::bad_cast
if locale doesn't contain a facet of typeFacet
std::runtime_error
in various null-pointer/undefined-facet situations
deque
/vector
length_error
: attemptreserve()
or implicitly-grow >max_size()
out_of_range
:at()
map
std::out_of_range
:at()
Well, I have this big, gigantic book titled, The C++ Standard, that contains a complete description of all functions in the standard library and what they can/cannot do.
精彩评论