On Aug 8, 3:50 am, SpreadTooThin <bjobrie...@gmail.comwrote:
I have a constructor that is being called by a global application
variable.
That constructor is throwing an exception.
As I am not in main() at that point I have no corresponding catch for
that exception.
Ok so what do I do?
Where you currently have this:
SomeClass gGlobal;
void foo()
{
gGlobal->use();
}
int main()
{
foo();
}
try this:
extern SomeClass& gGlobal();
void foo()
{
gGlobal()->use(); /* nb parens */
}
SomeClass& gGlobal()
{
static SomeClass sGlobal;
return sGlobal;
}
int main()
{
try
{
foo();
}
catch(...)
{
// whatever
}
}
I'm not sure that this is a guaranteed solution as I think
an implementation is still allowed to instantiate sGlobal before
main() is entered, in which case you're back to square one,
but perhaps someone more knowledgeable can adjudicate. Similarly,
I'm not sure what's guaranteed in a multi-threaded environment.