Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a list of STL container methods that may throw an exception anywhere?

Tags:

c++

stl

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?

like image 537
Tony Park Avatar asked Nov 25 '10 01:11

Tony Park


2 Answers

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 long
    • std::out_of_range - operator[]() attempt past end
  • new
    • std::bad_alloc
  • typeinfo
    • std::bad_cast on invalid dynamic_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 exceed max_size() during reserve or implicit resize (e.g. assign/insert/+= etc.)
  • locale
    • std::bad_cast if locale doesn't contain a facet of type Facet
    • std::runtime_error in various null-pointer/undefined-facet situations
  • deque/vector
    • length_error: attempt reserve() or implicitly-grow > max_size()
    • out_of_range: at()
  • map
    • std::out_of_range: at()
like image 165
Tony Delroy Avatar answered Oct 05 '22 10:10

Tony Delroy


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.

like image 36
Edward Strange Avatar answered Oct 05 '22 12:10

Edward Strange