Re: C++ rule on function-try-block
On 10/11/2010 22:24, red floyd wrote:
On Nov 10, 9:30 am, 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?
I don't think so. I don't have my copy of the Standard handy, but
case 1 leaves a fully constructed X available. I *THINK* that Case 2
doesn't construct the X.
When an entire function is enclosed in a try block the catch is IIRC,
required to rethrow the exception. The rationale is that you have
already left the function and so there is no way for a normal return.
Now I know that ctors are different (IMHO they are not functions) but I
do not think the Standard recognises that difference.
IMO it would be an error if it behaved otherwise because if an exception
has been thrown in a ctor there is a problem which cannot be dealt with
after the ctor completes.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]