Click to See Complete Forum and Search --> : life cycle of exception object


George2
February 4th, 2008, 02:15 AM
Hello everyone,


1.

Suppose I have code like this. According to the standard, the storage of exception object is undefined (could be either on stack or on heap?).

2.

I am wondering what is the life cycle of the exception object which reference variable e binded to? For example, could we bind a global reference to the exception object referred by e? Or doing something on the exception object beyond the bracket?

3.

The life cycle of variable e itself should not beyond the bracket, right?


catch (exception& e)
{
// ...
}



thanks in advance,
George

code_carnage
February 4th, 2008, 03:50 AM
Hello everyone,


1.

Suppose I have code like this. According to the standard, the storage of exception object is undefined (could be either on stack or on heap?).

2.

I am wondering what is the life cycle of the exception object which reference variable e binded to? For example, could we bind a global reference to the exception object referred by e? Or doing something on the exception object beyond the bracket?

3.

The life cycle of variable e itself should not beyond the bracket, right?


catch (exception& e)
{
// ...
}



thanks in advance,
George
1. I dont think so, it could be either stack or heap. Depends on
whether you have done this.
exception* pt = new exception()
throw pt
or you have done this.

exception pt;
throw pt;

2. If you throw an exception compilar creates temprary object which is copy of the thrown object using copy constructor. The temporary is then passed to matching catch clause. If you catch by refrence, this time compilar allows non const refrence to be assigned to temporary. That means compilar relaxes a bit in case of exception.

So it means life of exception object is very much limited to the scope where it was created. What exists out of the scope is its copy.

Hope I answered your question.

Regards
Prashant.

George2
February 4th, 2008, 04:08 AM
Thanks Prashant,


Two more comments,

1. I dont think so, it could be either stack or heap. Depends on
whether you have done this.
exception* pt = new exception()
throw pt
or you have done this.

exception pt;
throw pt;

2. If you throw an exception compilar creates temprary object which is copy of the thrown object using copy constructor. The temporary is then passed to matching catch clause. If you catch by refrence, this time compilar allows non const refrence to be assigned to temporary. That means compilar relaxes a bit in case of exception.

You mean whether we use new or just use local exception object, inside the catch block, the exception object not the original one (variable pt), but a copy? I am interested in learning it. Could you prove they are different please?

2.


So it means life of exception object is very much limited to the scope where it was created. What exists out of the scope is its copy.

Hope I answered your question.

Regards
Prashant.

From your above analysis, I think from logical point of view, "What exists out of the scope is its copy" is not correct.

Suppose you have,

exception* pt = new exception();
throw pt;

then in catch block you have a copy of pt, after catch block you can not address the copy of pt, and you should address the pt itself -- it is using new and on heap.

So your statement "What exists out of the scope is its copy" should be "What exists out of the scope is not its temporary copy, but the original object (on heap)", right?


regards,
George

George2
February 4th, 2008, 04:43 AM
Hi Prashant,


I have written some code to prove the exception object we catch in the catch block is not the original one, but a copied temporary object instance. It is appreciated if you could review whether my code is correct or not.

It is also appreciated if you could review and comment my point (2) in post #3. :-)


#include <iostream>

using namespace std;

class Foo {
public:
Foo(int input)
{
this->a = input;
cout << this << endl;
}

Foo (const Foo& input)
{
this->a = input.a;
cout << this << endl;
}
int a;
};

void foo()
{
try {
Foo* f = new Foo (100);
throw *f;
} catch (const Foo& e)
{
cout << e.a << endl;
}
}

int main()
{
foo();
}


1. I dont think so, it could be either stack or heap. Depends on
whether you have done this.
exception* pt = new exception()
throw pt
or you have done this.

exception pt;
throw pt;

2. If you throw an exception compilar creates temprary object which is copy of the thrown object using copy constructor. The temporary is then passed to matching catch clause. If you catch by refrence, this time compilar allows non const refrence to be assigned to temporary. That means compilar relaxes a bit in case of exception.

So it means life of exception object is very much limited to the scope where it was created. What exists out of the scope is its copy.

Hope I answered your question.

Regards
Prashant.


regards,
George