Re: uncaught throw.. (exception)
* tragomaskhalos:
On Aug 8, 3:50 am, SpreadTooThin <bjobrie...@gmail.com> wrote:
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,
Only if gGlobal() is called before main() is entered. sGlobal is
instantiated the first time execution passes the declaration.
but perhaps someone more knowledgeable can adjudicate. Similarly,
I'm not sure what's guaranteed in a multi-threaded environment.
Depends on the C++ implementation; the C++ standard does not acknowledge
the existence of threads. But generally, one should not expect a C++
compiler to introduce locks or other costly checks. It's not in the
spirit of C++ (don't pay for what you don't use).
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?