#include #include #include using namespace std; struct truncation { int dividend; //data members public for simplicity int divisor; truncation(int initial_dividend, int initial_divisor) : dividend(initial_dividend), divisor(initial_divisor) {} }; class overflow {}; int main() { int status = EXIT_FAILURE; //guilty until proven innocent try { cout << "Please input the dividend and press RETURN: "; int dividend; //uninitialized variable cin >> dividend; cout << "Please input the divisor and press RETURN: "; int divisor; //uuninitialized variable cin >> divisor; if (divisor == 0) { throw dividend; } if (dividend == INT_MIN && divisor == -1) { const overflow ov = overflow(); throw ov; } if (dividend % divisor != 0) { const truncation t(dividend, divisor); throw t; } cout << "The quotient is " << dividend / divisor << ".\n"; status = EXIT_SUCCESS; } catch (int i) { cerr << "Attempt to divide " << i << " by zero.\n"; } catch (truncation t) { cerr << "Truncation would result when dividing " << t.dividend << " by " << t.divisor << ".\n"; } catch (overflow) { cerr << "Integer overflow would result when dividing " << INT_MIN << " by -1.\n"; } catch (...) { cerr << "Caught unexpected exception.\n"; } return status; }