exceptions (throw and catch)

this code :
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
  #include <iostream> 
#include <string>
using namespace std;
class Class {
public:
	string msg;
	Class(string txt):msg(txt){cout<<"Object [" << msg << "] constructed" << endl; }
	~Class(void) { cout << "Object [" << msg << "] destructed" << endl; }
	void Hello(void) { cout << "Object [" << msg << "] says: hello" << endl; }
};
void DoCalculations(int i) {
	if(i == 0) 
		throw Class("exception 1");
	Class object("object");
	if(i == 1)
		throw Class("exception 2");
	object.Hello();
	if(i == 2)
		throw Class("exception 3");
}
int main(void) {
    for(int i = 0; i < 3; i++) {
	try {
	   cout << "-------" << endl;
	   DoCalculations(i);
	} catch (Class &exc) {
	   cout << "Caught!" << endl;
	   cout << exc.msg << endl;
	}
    }	
    return 0;
}

is supposed to output this :
-------
Object [exception 1] constructed
Caught!
exception 1
Object [exception 1] destructed
-------
Object [object] constructed
Object [exception 2] constructed
Object [object] destructed
Caught!
exception 2
Object [exception 2] destructed
-------
Object [object] constructed
Object [object] says: hello
Object [exception 3] constructed
Object [object] destructed
Caught!
exception 3
Object [exception 3] destructed

but when i run it using my visual studio express 2012 (with update 4) it displays this output :
-------
Object [exception 1] constructed
Object [exception 1] destructed
Caught!
exception 1
Object [exception 1] destructed
-------
Object [object] constructed
Object [exception 2] constructed
Object [exception 2] destructed
Object [object] destructed
Caught!
exception 2
Object [exception 2] destructed
-------
Object [object] constructed
Object [object] says: hello
Object [exception 3] constructed
Object [exception 3] destructed
Object [object] destructed
Caught!
exception 3
Object [exception 3] destructed

it displays one more "Object [exception n] destructed" statement after each "Object [exception n] constructed" statement, why does it act like this ?
Last edited on
Implement a copy constructor for Class and print a unique message there and see if that gets printed
@codewalker : yeah i did that, i know that it is treating throwing an object like throwing its copy even if i put the & in the catch statement parameter, when i remove the & it makes 2 copies of the object and then removes both at the end of catch, i don't know why this happens
Last edited on
Keep the reference in catch
look at line #14 and #17
closed account (E0p9LyTq)
I suspect that Microsoft's C++ implementation might be a bit "nonstandard". I tried the code with TDM-GCC 4.92 and the output I received was as expected:

-------
Object [exception 1] constructed
Caught!
exception 1
Object [exception 1] destructed
-------
Object [object] constructed
Object [exception 2] constructed
Object [object] destructed
Caught!
exception 2
Object [exception 2] destructed
-------
Object [object] constructed
Object [object] says: hello
Object [exception 3] constructed
Object [object] destructed
Caught!
exception 3
Object [exception 3] destructed
Topic archived. No new replies allowed.