开发者

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

开发者 https://www.devze.com 2023-01-26 16:12 出处:网络
I know the STL will throw on a memory allocation error or if the contained type throws in its constructor / assignment operator.

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 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()


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.

0

精彩评论

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