C++之异常处理

来源:互联网 发布:python重构是什么意思 编辑:程序博客网 时间:2024/06/14 07:26
//编写一段代码,从标准输入读取两个整数,输出两数除的结果//如果第二数是0抛出异常//使用try去捕获异常//catch子句应该为用户输出一条提示信息,询问其是否输入新数并重新执行try语句块的内容#include <iostream>#include <stdexcept>using namespace std;int main(void){    int i, j,result;    cout << "Please input two integer:" << endl;    while (cin >> i >> j)    {        try {            if (j == 0)                throw runtime_error("j can't equals 0!");            result = i / j;            cout <<"The result is:"<< result << endl;        }        catch (runtime_error err) {            cout << err.what()                << "\nTry Again? Enter y or n" << endl;            char ch;            cin >> ch;            if (ch != 'y' && ch != 'Y')                break;            cout << "contiue to input tow integer:" << endl;        }    }    return 0;}