// File; DivBy0.cpp // Another Exception handling example #include #include using namespace std; // Divides divisor with dividend. // Precondition, dividend != 0 int divide(int divisor, int dividend) throw (string); int main(void) { try { int result = divide(50,2); cout << "divide(" << 50 << ", " << 2 << ") yields " << result << endl; result = divide(50,0); cout << "divide(" << 50 << ", " << 0 << ") yields " << result << endl; } catch (string msg) { cout << "Oops, caught: " << msg << endl; } return 0; } int divide(int divisor, int dividend) throw (string) { if (dividend == 0) throw (string)"Division by zero attempted"; // Here we don't have to worry about dividend being zero return divisor/dividend; }