The istream and ostream iterators by themselves have no means of indicating the success or failure of an operation. The stream being operated on should be probed for good(), eof(), fail(), or bad() conditions. This is convenient, as stream state can be checked for exception conditions that were set in basic_ios (using the member function exceptions(iostate)) after each input or output operation.
void print() { std::ostream_iterator<int, char, char_traits<char> > os(cout); try { std::cout.exceptions (std::ios::failbit | std::ios::badbit); *os = 3; //output `3' os++; // receive next output } catch(std::ios_base::failure& emsg) { std::cerr<<emsg.what(); } }
Since stream buffers do not retain information about the success or failure of operations, streambuf iterators provide a member function, failed(), to indicate success or failure in a preceding operation on the stream buffer.