// Code derived from Stroustrup's PPP2 book// § 10.6 I/O error handling// -and beginning on p 357#include<iostream>#include<stdexcept>#include<vector>usingnamespacestd;//------------------------------------------------------------------------------// read integers from ist into v until we reach eof() or terminatorvoidfill_vector(istream&ist,vector<int>&v,charterminator){for(inti;ist>>i;)v.push_back(i);if(ist.eof())return;// fine: we found the end of file// not good() and not bad() and not eof(); so ist must be fail() :ist.clear();// clear stream statecharc;ist>>c;// read a character, hopefully terminatorif(c!=terminator){// ouch: not the terminator, so we must failist.unget();// maybe my caller can use that characterist.clear(ios_base::failbit);// set the state to fail()}}intmain()try{istream&ist{cin};// makes ist throw if it goes badist.exceptions(ist.exceptions()|ios_base::badbit);vector<int>v;fill_vector(ist,v,'*');}catch(exception&e){cerr<<"error: "<<e.what()<<'\n';return1;}catch(...){cerr<<"Oops: unknown exception!\n";return2;}