How can I identify and solve the error in my exception handling?

Hello,

I am constructing a fractions calculator using classes. I have constructed the member functions with a try-catch-throw exception within a while loop to root out zero denominators. But when the compiler encounters one the member functions the console crashes :( when it meets a zero denominator instead of allowing me to enter new values for the fraction.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <iostream>

using namespace std;

class fractions{


private:
    int A,B,C,D, SUM;

public:
    fractions(int = 1, int = 1, int = 1, int = 1, int = 0);

    void show_fractions(float,float,float,float);
    float sum_of_fractions(float,float,float,float);
    float diff_of_fractions(float,float,float,float);
    float product_of_fractions(float,float,float,float);
    float division_of_fractions (float,float,float,float);
};

fractions::fractions(int a, int b, int c, int d, int sum)
{
    A = a;
    B = b;
    C = c;
    D = d;
    SUM = sum;

    cout << " The constructor has created 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "  " << B << " " << C << " "<< D << endl;
}

void fractions::show_fractions(float A,float B,float C, float D)
{
    cout    << " The fractions are "
            << A << " / " << B << " and " << C << " / " << D << endl;
}

float fractions::sum_of_fractions(float A,float B,float C, float D)
{
    float denominator = 0;
    float sum = 0;

    cout << " The  2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "/" << B << " and " << C << "/"<< D << endl;

        denominator = (B * D);

    cout << " The denominator of " << B << " x " << D << endl;

    bool denominator_not_ok = true;

    while (denominator_not_ok)
    {
        try
        {
            if ((denominator == 0))
            {
                cout << "denominator is a zero value" << endl;
                throw denominator;
            }
        }
        catch (int e)
        {
            cout << " What are the values of A?" << endl;
            cin >> A;

            cout << " What are the values of B?" << endl;
            cin >> B;

            cout << " What are the values of B?" << endl;
            cin >> C;

            cout << "What is the value of D?"<< endl;
            cin >> D;
        }
        denominator_not_ok = false;
    }

    sum = (((A * D) + (C * B)) / (B * D));

cout << " The sum of 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << "   ";
cout << A << "/" << B << " and " << C << "/"<< D << endl;
cout << " is " << sum << endl;

return sum;

}

float fractions::diff_of_fractions(float A,float B,float C, float D)
{

    float denominator = 0;
    float sum = 0;

    cout << " The  2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "/" << B << " and " << C << "/"<< D << endl;

    denominator = (B * D);

    cout << " The denominator of (" << B << " x " << D << ")"<< endl;

    bool denominator_not_ok = true;

    while (denominator_not_ok)
    {
        try
        {
            if ((denominator == 0))
            {
                cout << "denominator is a zero value" << endl;
                throw denominator;
            }
        }
        catch (int e)
        {
            cout << " What are the values of A?" << endl;
            cin >> A;

            cout << " What are the values of B?" << endl;
            cin >> B;

            cout << " What are the values of B?" << endl;
            cin >> C;

            cout << "What is the value of D?"<< endl;
            cin >> D;
        }
        denominator_not_ok = false;
    }

    sum = (((A * D) - (C * B)) / (B * D));

cout << " The difference between 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << "   ";
cout << A << "/" << B << " - " << C << "/"<< D << " = " << sum << endl;

return sum;
}

float fractions::product_of_fractions(float A,float B,float C, float D)
{
    float denominator = 0;
    float sum = 0;

    denominator = (B * D);

    cout << " The denominator is (" << B << " x " << D << ")"<< endl;

    bool denominator_not_ok = true;

    while (denominator_not_ok)
    {
        try
        {
            if ((denominator == 0))
            {
                cout << "denominator is a zero value" << endl;
                throw denominator;
            }
        }
        catch (int e)
        {
            cout << " What are the values of A?" << endl;
            cin >> A;

            cout << " What are the values of B?" << endl;
            cin >> B;

            cout << " What are the values of B?" << endl;
            cin >> C;

            cout << "What is the value of D?"<< endl;
            cin >> D;
        }
        denominator_not_ok = false;
    }

    sum = ((A * C) / (B * D));

cout << " The difference between 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << "   ";
cout << A << "/" << B << " x " << C << "/"<< D << " = " << sum << endl;

return sum;
}

float fractions::division_of_fractions(float A,float B,float C, float D)
{
    float denominator = 0;
    float sum = 0;

    cout << " The  2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "/" << B << " and " << C << "/"<< D << endl;

    denominator = (B * C);

    cout << " The denominator of (" << B << " x " << C << ")"<< endl;

    bool denominator_not_ok = true;

    while (denominator_not_ok)
    {
        try
        {
            if ((denominator == 0))
            {
                cout << "denominator is a zero value" << endl;
                throw denominator;
            }
        }
        catch (int e)
        {
            cout << " What are the values of A?" << endl;
            cin >> A;

            cout << " What are the values of B?" << endl;
            cin >> B;

            cout << " What are the values of B?" << endl;
            cin >> C;

            cout << "What is the value of D?"<< endl;
            cin >> D;
        }
        denominator_not_ok = false;
    }

    sum = ((A * D) / (B * C));

cout << " The difference between 2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << "   ";
cout << A << "/" << B << " / " << C << "/"<< D << " = " << sum << endl;

return sum;
}

int main()
{
    float A,B,C,D;

    cout << "To calculate the sum, difference, product and division of 2 fractions in the form A / B and C / D you need to enter some numbers" << endl;

    cout << "What are the values of A?" << endl;
    cin >> A;

    cout << "What are the values of B?" << endl;
    cin >> B;

    cout << "What are the values of B?" << endl;
    cin >> C;

    cout << "What is the value of D?"<< endl;
    cin >> D;

    cout << "The  2 fractions in the form a/b and c/d with the following values for a,b,c and d respectively" << endl;
    cout << A << "/" << B << " and " << C << "/"<< D << endl;

    fractions AA;

    //A.show_fractions(A,B,C,D);
    AA.sum_of_fractions(A,B,C,D);
    AA.diff_of_fractions(A,B,C,D);
    AA.product_of_fractions(A,B,C,D);
    AA.division_of_fractions(A,B,C,D);

    return 0;
}
1. Don't post the same question multiple times.

2. You don't need to use try catch blocks. Just use plain if statements.
Hello Boost,

Sorry, that's a school boy forum error move I know. I was just using a framework from a textbook I am using to teach myself C++. Is using if-else a better way that try-catch-throw?

I will give it a go. It's probably just a case of changing over a few commands in my current code.
Is using if-else a better way that try-catch-throw?
Yes.

In your case you catch an int value, but you do not throw an int hence you will not catch your own throw. You can simply put the catch block within the if block.

You always execute this line:

denominator_not_ok = false;

Hence the loop will always run only once. You may set it true in the else case.
One obvious problem is the following:

Lines 60,112,158,207: You're throwing a float
Line 63,115,161,210: You're trying to catch an int.
Your catch block will never be invoked.

IMO, try/catch blocks are seldom useful within the same function. Exceptions should be used when a function has exhausted all error handling and wants to communicate the failure to higher level code. As boost lexical cast said, use if statements.

Lines 49-78, 101-130, 147-176, 196-225: Do you see a lot of repeated code here? That should be an indication to you that you need a function.

1
2
3
4
5
6
7
8
float fractions::check_denominator ()
{   cout << " The denominator is (" << B << " x " << D << ")"<< endl;    
    while (B * D == 0)  
    {  cout << "Denominator is 0" << endl;
        //   reenter A-D    
    }
    return B * D;
}


Last edited on
Hello AbstractionAnon,

Those tips have been very helpful and it all works a treat now! As for the code repetition, I have since moved on to a new chapter about static member functions. So I can now see how to implement your suggestion as such.
Topic archived. No new replies allowed.