Can stringstream throw an exception when reading a primitive?

Looking at some old code, we have many things, like the following:

// This is dumb string do_something(int in) { stringstream out; try { out << std::fixed << in; } catch(std::exception &e) { out << e.what(); } return out.str(); } // Can't we just do this? Can this ever fail? string do_something_better(int in) { stringstream out; out << std::fixed << in; return out.str(); } 

When stringstream reads a primitive, can it ever throw an exception? What about reading a line?

+7
source share
2 answers

Summing up several answers

By default, threads do not throw exceptions. They can, if included.

 stringstream out; out.exceptions(std::ios::failbit); // throw exception if failbit gets set 

According to the Apache C ++ Standard Library User Guide

The std :: ios_base :: badbit flag indicates problems with the underlying stream buffer. These problems may include the following:

Out of memory . There is no memory to create the buffer, or the buffer has a size of 0 for other reasons (for example, is provided outside the stream) or the stream cannot allocate memory for its own internal data, as is the case with std :: ios_base :: iword () and std :: ios_base :: pword ().

The base stream buffer throws an exception. A stream buffer may lose its integrity, for example, due to a lack of memory or a code conversion failure, or a recovery error from an external device. The stream buffer may indicate this loss of integrity by throwing an exception that is caught by the stream and causes the badbit to be set in the stream state.

Generally, you should keep in mind that badbit indicates an error situation that may not be recoverable, while failbit indicates a situation that may allow you to retry an unsuccessful operation.

So it seems that the safest way to do this is

 string do_something(int in) { stringstream out; // This could throw a bad_alloc out << std::fixed << in; // This could set bad or fail bits if(out.good()) { return out.str(); } else { return ""; } } 

This is too tedious, because according to Bad_alloc Handling , if thread creation fails, there are big problems to worry about, and the program is probably about to exit. Thus, assuming that it begins to create a stream, it is possible, but extremely unlikely, that the bit will be installed. (The thread receives the allocation with memory <sizeof (int)).

It is also unlikely that failbit will be installed (not sure if it is used to read from the stack, except for the corrupt stack). Thus, the following code is sufficient, since recovery from a stream error is currently inactive.

 string do_something(int in) { stringstream out; out << std::fixed << in; return out.str(); } 
+7
source

All threads, including istringstreams , can throw exceptions (managed by ios::exceptions ) when reading, for example. when they run out of input. Plus, they can throw when memory runs out (for example, when building a line that is currently being read).

However, your sample code writes (?) To an AFAIK record and int should not contain any exceptions except for obvious errors in memory (which your code does not handle very well).

+2
source

All Articles