// Code derived from Stroustrup's PPP2 book// § 10.6 I/O error handling// -and beginning on p 355#include<iostream>#include<stdexcept>#include<string>usingnamespacestd;voiderror(conststrings){throwruntime_error(s);}voiderror(conststrings1,conststrings2){error(s1+s2);}//------------------------------------------------------------------------------intmain()try{inti=0;cin>>i;if(!cin){// we get here (only) if an input operation failed:if(cin.bad())error("cin is bad");// stream corrupted: let’s get out of here!if(cin.eof()){// no more input// this is often how we want a sequence of input operations to end}if(cin.fail()){// stream encountered something unexpectedcin.clear();// make ready for more input// somehow recover . . .}}}catch(exception&e){cerr<<"error: "<<e.what()<<'\n';return1;}catch(...){cerr<<"Oops: unknown exception!\n";return2;}