Is there a reference about C++ Standard Library Exceptions? I just want to know that which functions may throw an exception or not.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
out_of_rangeif the index provided is invalid:std::vector<>::at()std::basic_string<>::at()std::bitset<>::set(),reset()andflip().std::overflow_erroron integer overflow:std::bitset<>::to_ulong()and (C++0x)to_ullong().std::allocator<T>will pass onstd::bad_allocthrown bynewwhich it invokes.std::ios_base::failureare thrown when a state bit is set.std::bad_array_new_lengthstd::bad_cast(technically not part of the standard library)std::bad_exceptionstd::function::operator(...)if it has no value will throwstd::bad_function_call.typeinfoof a null pointer may throw astd::bad_typeid.weak_ptrafter the pointee has been released will throw astd::bad_weak_ptr.std::promise/std::futuremay throw astd::future_error.std::stoi,std::stol,std::stoll,std::stoul,std::stoull,std::stof,std::stod, andstd::stoldcan throw bothstd::invalid_argumentandstd::out_of_range.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.