Why do std :: stof, std :: stod and std :: stold handle errors with exceptions?

What is the reason why std :: stof, std :: stod, std :: stold throw exceptions?

http://en.cppreference.com/w/cpp/string/basic_string/stof

Input errors are a commonly used example where error handling with exceptions is a poor fit (usually a phrase with fancy circular reasoning about “exceptional circumstances”, but still a good example). It is not as if other error handling mechanisms were somehow verbothed in the C ++ standard library. For example, another C ++ 11 newbie, the std::unordered_map::insert family, indicates a failure using the second element in the return type std::pair<iterator,bool> . Failure inside the std::unordered_map::insert function seems much more "exceptional" than an input error. Without trying to insert, you can guarantee that the insert will be successful, but indiscriminately it is impossible to guarantee that the parsing will be successful.

I'm just wondering what the rationale was at the time when these functions were accepted into the standard. I hope it is published somewhere, or a member of the committee can drop by and shed some light on this. I do not ask for a detailed treatise on the advantages and disadvantages of exceptions in comparison with other mechanisms.

+6
source share
1 answer

Original paper, N1803: Simple digital access uses exceptions. However, the document does not say anything about where any part of their design comes from (for example, why they completely ignore distributors!). Later changes to it ( N1982 , N2408 ) also do not say anything about why they throw exceptions. If there is another entry behind these documents, I do not know about it.

However, I can take a chance. We see that there are exceptions in the very first draft of the article. And it does not seem to be in dispute. This is probably due to the notion that exceptions are the standard method for indicating the failure of an operation in C ++ and, in particular, the standard C ++ library.

Some standard library types have other error mechanisms (iostreams). But, generally speaking, exceptions are the default case.

+6
source

All Articles