junw2000@gmail.com wrote:
Thanks.
http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.3
I read the faq. Faq17.3 says that "For example, if someone says throw
Foo(), the stack will be unwound so all the stack frames between the
throw Foo() and the } catch (Foo e) { will get popped. This is called
stack unwinding."
But I can not understand it. When throw Foo(), the 'throw Foo()' is
located at the top of the stack, right? Where is 'catch (Foo e)' in the
stack?
Mr. Cline is describing what happens under the hood on many platforms.
There is no language requirement that a system stack even exist.
Perhaps, clearer would be this:
struct E1 {};
struct E2 {};
struct A { ~A() { throw E1(); } };
// ...
try
{
A a;
throw E2();
}
catch( const E1& e )
{
// ... never reached ...
}
catch( const E2& e )
{
// ... never reached ...
}
When MyException is thrown, a's destructor is called, but it also
throws an exception. Now, as per the language definition, you can only
handle one of these two exceptions at a time. So which one (E1 or E2?)
should the language pass on to your handlers? Since handling one means
the other is necessarily unhandled, the language just terminates the
program rather than try to decide for you.
Are the } and { typos in the sentence above?
He just means "catch( Foo e )" (or better, "catch( const Foo& e )",
which is in line with another FAQ:
http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.7).
Whenever an exception is thrown, does stack unwinding happen?
It's technically implementation dependent, but commonly, yes.
Cheers! --M
Thanks a lot. I understand now. I make some changes to your code as
// ...
// ... CAN BE reached ... //LINE3
// ... never reached ...
I comment LINE1. When LINE2 is reached, the object a will be destroyed,
since a is only valid within the try{} block. But this is NOT stack
unwinding. LINE0 will throw an exception. LINE3 can be reached. Is my