uncaught throw.. (exception)

S

SpreadTooThin

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?
 
A

Alf P. Steinbach

* SpreadTooThin:
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?

Simply don't use globals.

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?
No.


Or is this blind faith?

No, it's design.
 
T

tragomaskhalos

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.
 
A

Alf P. Steinbach

* tragomaskhalos:
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).
 
B

BobR

SpreadTooThin said:
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'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top