Re: C++ rule on function-try-block
On Nov 10, 12:30 pm, muler <mulugeta.abe...@gmail.com> wrote:
Hi all,
[ISO/IEC 14882:1998] A function-try-block associates a handler-seq
with the ctor-initializer, if present, and the function-body. An
exception thrown during the execution of the initializer expressions
in the ctor-initializer **or during the execution of the function-body
transfers control to a handler in a function-try-block in the same way
as an exception thrown during the execution of a try-block transfers
control to other handlers.**
To demonstrate the statement in between **...**, I cooked up the
following example (using VC++2010):
Given:
=====
class GenericError {};
void Print(int x)
{
if (x < 0) throw GenericError();
}
class X {
public:
X();
};
Case 1: // no error executing this
=====
X::X()
{
try
{
Print(-1);
}
catch(const GenericError& ge)
{
}
}
Case 2: // error while trying to execute this
======
X::X() try
{
Print(-1);}
catch(const GenericError& ge)
{
}
According to the standard, the behavior for Case 1 and Case 2 should
be the same, right? But it is not in VC++2010. Is this a compiler
issue or what?
Thanks,
Muler
Sorry, I accidentally clicked the "Send" button just now, if the
moderator can see this, please discard my previous reply, thank you.
{ we process articles in the order they arrive. if you need one of
your earlier posts rejected, wait for the confirmation on it and
then ask moderators to reject. thanks. -mod }
According to my understanding of the standard, your second case falls
into 15.3.16 :
"The exception being handled is rethrown if control reaches the end of
a handler of the function-try-block of a constructor or destructor.
Otherwise, a function returns when control reaches the end of a
handler for the function-try-block (6.6.3). Flowing off the end of a
function-try-block is equivalent to a return with no value; this
results in undefined behavior in a value-returning function (6.6.3)."
So what happened is an exception is thrown and then caught/handled and
then automatically rethrown when control flows off the end of the
handler, and then according to 15.1.6 :
"A throw-expression with no operand rethrows the exception being
handled. The exception is reactivated
with the existing temporary; no new temporary exception object is
created. The exception is no longer considered to be caught;
therefore, the value of uncaught_exception() will again be true. "
So when control exits your handler, the exception is no longer
considered to be caught which I think differs from your first case
where the exception is considered to be caught due to the
corresponding catch clause.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]