64,649
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <exception>
using namespace std;
int main(){
try{
for(int i(0);i!=10;++i){
int j(0);
cout<<"next integer value\t";
cin>>j;
if(not cin) throw exception();
cout<<i<<"\t"<<j<<endl;
}
}
catch(...){
cerr<<"Caught an exception"<<endl;
//return 0;
}
system("pause");
}
#include <iostream>
#include <exception>
using namespace std;
int main()
{
try
{
for(int i=0;i!=10;++i)
{
int j=0;
cout<<"next integer value\t"<<flush;
cin>>j;
if(!cin)
{
throw exception("Caught an exception");
}
//cout<<i<<"\t"<<j<<endl;
cout<<j<<endl;
//打印:3,然后进入下一次迭代,读了一个非int的",",cin==false,抛出异常
}
}
catch(exception err)
{
cerr<<err.what()<<endl;
return -1;
}
return 0;
}