# what does stringstream do on failure?

This topic is 4885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What will happen if I run code like this:
std::stringstream s;
int i;
s << "HELLO";
s >> i;

Does it throw an exception? It would also be helpful if somebody could point me to the section in the C++ standard which specifies this behaviour.

##### Share on other sites
Quote:
 Original post by NitageDoes it throw an exception?

By default, it sets the fail bit on the stream. You can request it to throw an exception instead with std::basic_ios::exceptions()

Quote:
 It would also be helpful if somebody could point me to the section in the C++ standard which specifies this behaviour.

Stream classes are addressed in sections 27.4 to 27.8.
String streams are addressed in section 27.7.

std::basic_ios is in section 27.4.4.

##### Share on other sites
basic_istringstream, basic_ostringstream and basic_stringstream are all descendants of ios_base. In this regard it should return a 'badbit' if the stream fails completely, otherwise it would return a 'failbit' if the stream is generally OK but the operation was not processed correctly. My view is that what you've shown would result in a failbit.

These can be used to throw an exeception, but I don't think that an exception is thrown automatically. This is what I can glean from Josuttis, 'The C++ Standard Library'.

--random_thinker

##### Share on other sites
It does not "return" anything, but instead those state bits are set within the stream object. Thus 'i' is not changed by the operation (and is thus still an uninitialized value), and further attempts to read from the stream will similarly fail, unless/until you .clear() the stream (and probably also .ignore() the data that caused the problem). Just like any other stream.

Keep in mind that if you're constructing the stringstream from data that in turn came from some other stream, and you want to throw away all that data on failure (say you're reading a line at a time from stdin, and want to skip lines until the user finally gets it right ;) ), you could just construct an entirely separate stringstream from the next bit of data:

int berateUserUntilIGetAnInt() {  int result;  cout << "Give me an int!" << endl;  while (true) {    string s;    getline(cin, s);    stringstream ss(s);    if (ss >> result) return result;    // Otherwise, got some bad input. Berate user and try again.    // The stringstream gets its dtor called here, and next time through    // it will be constructed again from the next string of input.    // Er, I think that works... don't have access to a compiler ATM ^^;    cout << "No, I said an int, you moron!" << endl;  }}

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634047
• Total Posts
3015228
×