intmain(){int a, b;
cin >> a >> b;if(0== b){throwruntime_error("b is 0!");}return0;}
运行如下
terminate called after throwing an instance of 'std::runtime_error'
what(): b is 0!
练习5.24
intmain(){int a, b;
cin >> a >> b;if(0== b){throwruntime_error("b is 0!");}return0;}
运行如下
terminate called after throwing an instance of 'std::runtime_error'
what(): b is 0!
练习5.25
intdivision(int a,int b){if(0== b){throwruntime_error("b is 0!");}return a / b;}intmain(){int a, b;while(cin >> a >> b){try{
cout <<division(a, b)<< endl;}catch(runtime_error &err){
cout << err.what()<<"\nTry Again? Enter y or n"<< endl;char c;
cin >> c;if(!cin || c =='n')break;}}return0;}