Runtime_error

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <stdexcept>

int main()
{
    int first = 0;
    int second = 0;
    while (first != -1)
    {
        std::cout << "Enter a number: ";
        std::cin >> first;
        std::cout << "Enter a second number: ";
        std::cin >> second;
        try
        {
            std::cout << first / second;
        }
        catch (std::runtime_error err)
            {
                std::cout << err.what();
                std::cout << "Try again?";
                char c;
                std::cin >> c;
                if (!std::cin || c == 'n')
                    break;
        }
    }
}
AFAIK, the C++ standard mentions nothing about exceptions thrown when dividing by zero.

According to the C++ standard (5.6)
If the second operand of / or % is zero the behavior is undeļ¬ned;

So division by zero may throw any of the STL exceptions, or might throw none. You can't rely on it to, though.

What you can do is throw your own runtime error if second is zero.
Last edited on
do

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main(int nNumberofArgs,char* pszArgs[])
{
    double first = 0;
    double second = 0;
    cout << "Division calculator: (Enter 0 for second to quit)" << endl;
    for(;;)
    {
        cout << "Enter a number: ";
        cin >> first;
        cout << "Enter a second number: ";
        cin >> second;
        if(second == 0)
        {
            cout << "DIVISION BY 0 IS NOT ALLOWED!" << endl;
            break;
        }
        cout << first << " / " << second << " = " << first / second << endl;
    }
    return 0;
}

what book was that?
C++ Primer 5th edition.

There are virtually no program examples, So it it's a long slow process learning from a reference book.
C++ Primer 5th edition doesn't have that code.

What it has is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.
Thank's Cubbi.
Topic archived. No new replies allowed.