throw wth no operand

B

Ben

I have been experiencing which I've managed to simplify down to the
following code. Is my assertion in the code misplaced? My compiler
(MSVC++ 6) hits the assert when I believe it should just abort after
the second throw.

#include <windows.h>
#include <assert.h>

void anotherFunction()
{
int *p = 0;
if(!p)
throw 1;

assert(false); // shouldn't get here, should we?
}

int main()
{
try
{
anotherFunction();
}
catch(int)
{
throw;
}
return 0;
}

Regards
Ben
 
A

Alf P. Steinbach

Ben skrev i meldingen ...
I have been experiencing which I've managed to simplify down to the
following code. Is my assertion in the code misplaced? My compiler
(MSVC++ 6) hits the assert when I believe it should just abort after
the second throw.

MSVC 6 has a number of bugs relating to exception handling, especially
rethrowing (e.g. an exception's destructor might be called twice).


#include <windows.h>
#include <assert.h>

void anotherFunction()
{
int *p = 0;
if(!p)
throw 1;

assert(false); // shouldn't get here, should we?
}

int main()
{
try
{
anotherFunction();
}
catch(int)
{
throw;
}
return 0;
}

Are you sure you're hitting the assert and not just invoking the general
top-level handler supplied by the compiler?

Try some flushed output.

It might be that you need to catch that exception as unsigned int -- but
instead, use std::runtime_error.
 
B

Ben

Are you sure you're hitting the assert and not just invoking the general
top-level handler supplied by the compiler?

Yes, definitely hitting the assert.
It might be that you need to catch that exception as unsigned int -- but
instead, use std::runtime_error.

Catching it as unsigned int caused an unhandled exception (not
surprisingly I has to chang the "throw 1;" to "throw 1u;" to make it
work again).

Interestingly, if I rethrow the exception in main using

catch(int i)
{
throw i;
}

....it causes an unhandled exception (fair enough) and then generates
the error about "not saving the value of ESP across function calls".
This suggests to me that the compiler has generated incorrect assembly
code. Any thoughts on that?

If I handle it in main like this

catch(int i)
{
throw;
}

....the throw exectution to immediately jump back to the line after the
original "throw 1;" statement. Which you'll see (from my original
post) is the "assert(false);" statement.

Confused
Ben
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top