I'm trying to catch division by 0 (zero) using runtime_error. The program functions ok until i enter zero as the second number, Then i get a window pop-up asking me to close the program.
I expect i'm doing something fundamentaly wrong, But i've entered the text as it was in the book.
while (cin >> item1 >> item2) {
try {
// execute code that will add the two Sales_items
// if the addition fails, the code throws a runtime_error exception
} catch (runtime_error err) {
// remind the user that the ISBNs must match and prompt for another pair
cout << err.what()
<< "\nTry Again? Enter y or n" << endl;
char c;
cin >> c;
if (!cin || c == 'n')
break; // break out of the while loop
}
}
where the comment about throwing refers to the previous chapter, which has
1 2 3 4 5
// first check that the data are for the same item
if (item1.isbn() != item2.isbn())
throw runtime_error("Data must refer to same ISBN");
// if we're still here, the ISBNs are the same
cout << item1 + item2 << endl;
Your code does not attempt to throw any exceptions, which is why nothing is caught.