Try/Catch not Working as Expected

I am working through examples in Chapter 14 of Malik's "C++ Computer Programming". This chapter deals with exceptions.

I am compiling in Cygwin with GCC version 4.5.3.

Code is as follows:

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
  #include <iostream>
#include <string>

using namespace std;

int main () {
	int dividend, divisor = 1, quotient;

	string inputStr
		= "The input stream is in a failure state.";

	try {
		cout << "Line 4: Enter the dividend: ";
		cin >> dividend;
		cout << endl;

		cout << "Line 7: Enter the divisor: ";
		cin >> divisor;
		cout << endl;

		if (divisor == 0)
			throw divisor;
		else if (divisor < 0)
			throw string("Negative Divisor.");
		else if (!cin)
			throw inputStr;

		quotient = dividend / divisor;

		cout << "Line 17: Quotient = " << quotient
			<< endl;
	}
	catch (int x) {
		cout << "Line 19: Division by " << x
			<< endl;
	}
	catch (string s) {
		cout << "Line 21: " << s << endl;
	}
	return 0;
}


The book says that I should expect bad (non integer) input to trigger the final catch block, but when I compile and run the program it returns the div-by-zero error. I suspect that the input is being coerced and is catching based on that, but I am unsure why that is the case. I checked and re-checked that I typed the example in correctly, but it is possible that I just typed something in wrong.

I would like it if someone could look this over and tell my why my experience is different from what the book tells me to expect.
I am getting the same output you are getting. However I would test the stream state first, then it would work properly.

Also even the way the program is now written if you enter a character in the first input you get the !cin state error.

Using an online compiler and entering 'a' at the first prompt yields for output:

Line 4: Enter the dividend: 
Line 7: Enter the divisor: 
Line 21: The input stream is in a failure state.


as expected. What did you enter at the prompts when it didn't work?

Also entering 2, and 'a' at prompts yields the same output.

Entering 2, 0 yields:

Line 4: Enter the dividend: 
Line 7: Enter the divisor: 
Line 19: Division by 0


Entering 2, -1 yields:

Line 4: Enter the dividend: 
Line 7: Enter the divisor: 
Line 21: Negative Divisor.

Last edited on
Entering 5, p gives me:

Line 3: Enter the dividend: 5
Line 6: Enter the divisor: p
Line 14: Division by 0.


Funny story: I copied the source from gVim into VS10, compiled & ran there, and it works as expected.

Any guesses why compiling with GCC would show such different behavior?

Incidentally, I would love to use a more recent version of GCC, but I think 4.5.3 is as far as Cygwin supports right now.
This online compiler is using GCC version 4.7.2:

http://www.compileonline.com/compile_cpp_online.php

Copy and paste your code into the main.cpp window.
Then click on input.txt and replace the text with 5 and p separated by a space.
Next, click on the main tab and then click Compile & Execute.

The results over on the left pane are as expected.

Perhaps someone else has heard of this bug.

Topic archived. No new replies allowed.