一个异常抛出的例子

#include"iostream"
#include"stdexcept"

using namespace std;

int main(){
    int num, num2;
    while (cin >> num >> num2) {
        try {
            if (num2 == 0)
                throw runtime_error("The num2 cannot be 0");
            cout << num / num2 << endl;
         catch (runtime_error err) {
                cout << err.what()
                    <<" Try Again? Enter y or n" << endl;
                char c;
                cin >> c;
                if (!cin || c == 'n')
                    break;
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wuOverflow/p/4098765.html