Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Exception in Ctor of Local Static Object

2 views
Skip to first unread message

Mikhail Kimmelman

unread,
Jun 14, 2004, 5:16:56 PM6/14/04
to
Hi All
I am thinking about the following problem:

try {
static Foo foo; // ctor may throw an exception
} catch (...) {
;
}

Does the standard guarantee that the exception thrown by Foo ctor
will be caught ?

6.7.4 allows early initialization of local objects with static storage
under certain conditions provided by 3.6.2

3.6.2 says : "the static version of the initialization produces
the same value in the initialized object as would be produced by
the dynamic initialization if all objects not required to be initialized
statically were initialized dynamically".

Does it mean that if a local static object ctor may throw an exception
then early initialization of that object is not allowed ?

Regards,
Misha

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Paul D. DeRocco

unread,
Jun 15, 2004, 5:05:09 AM6/15/04
to
> "Mikhail Kimmelman" <m_kim...@hotmail.com> wrote

>
> I am thinking about the following problem:
>
> try {
> static Foo foo; // ctor may throw an exception
> } catch (...) {
> ;
> }
>
> Does the standard guarantee that the exception thrown by Foo ctor
> will be caught ?
>
> 6.7.4 allows early initialization of local objects with static storage
> under certain conditions provided by 3.6.2
>
> 3.6.2 says : "the static version of the initialization produces
> the same value in the initialized object as would be produced by
> the dynamic initialization if all objects not required to be initialized
> statically were initialized dynamically".
>
> Does it mean that if a local static object ctor may throw an exception
> then early initialization of that object is not allowed ?

I believe that by static initialization, or early initialization, the
standard means that the object's value may be built into the memory image of
the program. The only things that can be part of the memory image are things
that can be computed by the compiler, and that would seem to rule out
anything that could throw an exception.

Besides, 6.7 p4 mentions the possibility of an exception being thrown while
computing the value for the static object, and says that the computation
will be retried if the declaration is ever reexecuted. If the computation
were allowed to occur early, it would result in an unhandled exception, so
could never be retried. I think the intent is clear.

--

Ciao, Paul D. DeRocco
Paul mailto:pder...@ix.netcom.com

0 new messages