// exceptdemo.cpp - shows a bit of exception handling // cmc, 4/6/2011 #include #include using namespace std; // udiv returns unsigned quotient of same-signed n and d // throws string if d is 0 // throws different string if n and d do not have the same sign unsigned int udiv(int n, int d) { if ( d == 0 ) throw string("d is 0"); if ( n * d < 0 ) throw string("n and d not same sign"); return n/d; } int main() { int n, d; cout << "I divide two integers of the same sign, n and d (with d != 0)"; cout << endl << "Enter n and d: "; cin >> n >> d; try { unsigned int result = udiv(n, d); cout << "n/d is " << result << endl; } catch (string e) { cerr << "Exception: " << e << endl; } return 0; }