#include #include #include //for strcmp #include using namespace std; struct truncation { int dividend; int divisor; truncation(int initial_dividend, int initial_divisor) : dividend(initial_dividend), divisor(initial_divisor) {} }; class overflow {}; void f() throw (int, overflow, truncation); //exception specification int main(int argc, char **argv) { int status = EXIT_FAILURE; const bool verbose = argc >= 2 && strcmp(argv[1], "-v") == 0; try { f(); status = EXIT_SUCCESS; } catch (int i) { if (verbose) { cerr << "Attempt to divide " << i << " by zero.\n"; } } catch (const truncation& t) { if (verbose) { cerr << "Truncation would result when dividing " << t.dividend << " by " << t.divisor << ".\n"; } } catch (overflow) { if (verbose) { cerr << "Overflow would result when dividing " << INT_MIN << " by -1.\n"; } } catch (...) { if (verbose) { cerr << "Caught unexpected exception.\n"; } } return status; } void f() throw (int, overflow, truncation) { cout << "Please input the dividend and press RETURN: "; int dividend; //uninitialized variable cin >> dividend; cout << "Please input the divisor and press RETURN: "; int divisor; //uninitialized variable cin >> divisor; if (divisor == 0) { throw dividend; } if (dividend == INT_MIN && divisor == -1) { throw overflow(); } if (dividend % divisor != 0) { throw truncation(dividend, divisor); } cout << "The quotient is " << dividend / divisor << ".\n"; }