// exception1.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program gives an introduction to exceptions and exception handling. // Exceptions are run-time program anomalies such as division by zero, // arithmetic or array overflow, the exhaustion of free store, ..etc. // In specific the program introduces "assert". An assert statement is // placed in a program to insure that some condition will never occur. // If this condition occurs, then the program will immediately terminate. // Notice that this is not the best way to handle an exception. Why? // Key Points: 1) assert #include #include void divOperations( ) { int x , y, counter = 0; cout << "You will be allowed to perform up to five divisions" << endl; while ( counter < 5) { cout << "Division # " << ++counter << ". Enter two integers to divide: "; cin >> x >> y; assert ( y !=0 ); // make sure that y is not 0 cout << "The result of the division is: " << x / y << endl; } } int main() { divOperations( ); return 0; } // The result of running the program /* You will be allowed to perform up to five divisions Division # 1. Enter two integers to divide: 8 2 The result of the division is: 4 Division # 2. Enter two integers to divide: 9 6 The result of the division is: 1 Division # 3. Enter two integers to divide: 8 0 Assertion failed: y !=0, file exception1.cpp, line 28 abnormal program termination occurred here (program crashed) */