Chapter 9 Completing the Basics
description
Transcript of Chapter 9 Completing the Basics
Chapter 9Completing the Basics
C++ for Engineers and Scientists
Third Edition
#include <iostream>using namespace 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;}
Run Time ErrorsRun Time Errors
Catch the errorCatch the error
#include <iostream>using namespace 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
Exception Handling
P. 518, Ex. 6
Send program and outputs To: [email protected] Subject: 32110236, CS125, 2013.5.7
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: [email protected] Subject: 32110236, CS125, 2013.5.7