By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,805 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

uncaught throw.. (exception)

P: n/a
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?

I could modify the constructor not to throw an exception...

What else could be done?

Can a method know if there is anyone around to catch the exception
that is about to be thrown?
Or is this blind faith?

Aug 8 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.


Aug 8 '07 #2

P: n/a
On Aug 8, 1:24 pm, tragomaskhalos <dave.du.verg...@logicacmg.com>
wrote:
>
Where you currently have this:
[code snipped]
Duh, for "->" read ".".


Aug 8 '07 #3

P: n/a

SpreadTooThin <bj********@gmail.comwrote in message...
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.
Put the 'try-catch' inside the global object?
Put the global object inside something (with 'try-catch')?
Don't use globals.
>
Ok so what do I do?
I could modify the constructor not to throw an exception...
Or put an 'function level try block' on the constructor.

class Bob{ public:
Bob( size_t const sz ) try : biggy( new int[ sz ] ) {
// if it got here, 'biggy' was probably allocated
}
catch( std::bad_alloc const &){
// handle it
std::exit(0);
}
catch( ThrownType const & ) {
// handle it
// throw SomethingElse("Bob puked!"); // but, same problem.
std::terminate();
}
int *biggy;
};

What else could be done?
Depends on what you *are* trying to do. I can't see your code from here.
>
Can a method know if there is anyone around to catch the exception
that is about to be thrown?
Or is this blind faith?
No, it's 'catch 22'.

--
Bob R
POVrookie
Aug 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.