70 likes | 142 Views
C++ for Engineers and Scientists Third Edition. Chapter 9 Completing the Basics. Run Time E rrors. #include <iostream> using namespace std; int main() { int top, bottom; cout << "Enter the top number (whole number only): " ; cin >> top;
E N D
C++ for Engineers and Scientists Third Edition Chapter 9 Completing the Basics
Run Time Errors #include<iostream> usingnamespace std; int main() { int top, bottom; cout << "Enter the top number (whole number only): "; cin >> top; cout << "Enter the bottom number (whole number only): "; cin >> bottom; cout << top <<'/' << bottom << " = " << double(top)/ double(bottom) << endl; }
Catch the error #include<iostream> usingnamespace std; int main() { int numerator, denominator; try { cout << "Enter the numerator (whole number only): "; cin >> numerator; cout << "Enter the denominator(whole number only): "; cin >> denominator; if (denominator == 0) throw denominator; // an integer value is thrown else cout << numerator <<'/' << denominator << " = " << double(numerator)/ double(denominator) << endl; } catch(int e) { cout << "A denominator value of " << e << " is invalid." << endl; exit (1); // Abnormal exit } }
Exception Handling • General syntax of code required to throw and catch and exception: • catch an exception
Lab Ex. • P. 518, Ex. 6 • Send program and outputs • To: ChanT@ucj.edu.sa • Subject: 32110236, CS125, 2013.5.7
Lab Ex. • P. 518, Ex. 7 • Modify the program so that it continues to try to divide 2 numbers until the user enters the character q to quit. • Send program and outputs • To: ChanT@ucj.edu.sa • Subject: 32110236, CS125, 2013.5.7