I am really new to the try/catch/throw concept, and can't figure out what is wrong with my code. Any suggestions?
Code:
#include <iostream>
using namespace std;
string msgZero = "Zero denominator\n";
void inverse(long value, double& answer) {
if (value > 0) {
answer = 1.0/value;
}
else {
if (value == 0) {
throw msgZero;
}
else {
throw value;
}
}
return; }
void fraction (long n, long d, double& result) {
inverse(d, result);
result = n * result;
return;
}
int main () {
while (true) {
long numer, denom;
double ans;
cout << "Enter numerator and
denominator: ";
if ((cin >> numer >> denom) == 0) break;
try {
fraction(numer, denom, ans);
}
catch (char* str) {
cout << str; }
cout << "Skipped the first catch block"<< endl;
catch (long val) {
cout << "Negative denominator "<<
val<< endl; }
cout << “*********”<< endl;
}
return (0);
}
Comment