Discussion:
Problem with re-throw
(too old to reply)
Timothy Madden
2010-09-23 17:08:59 UTC
Permalink
Hello

I have an application where MFC callback functions use to catch
everythig with catch(...) and then simply call my GlobalErrorHandler()
that will throw; and catch the exception again, this time with a
specific exception type.

That is, GlobalErrorHandler knows about as many exception types as my
application can handle, and all callback functions just catch(...) once
without the specific type and then invoke GlobalErrorHandler(), which
will then get the type.

Recently I run into a strange problem with this approach, however. After
the rethrow the program auto-magically rebuilds the entire call stack
from the time of the original throw, and attempts to destroy local
variables in the call stack again. So my application then crashes.

Why would VC++ call destructors again at re-throw ? How can it rebuild
the call stack ? Has anyone run into this problem before ?

Thank you,
Timothy Madden
Cholo Lennon
2010-09-23 18:51:40 UTC
Permalink
Post by Timothy Madden
Hello
I have an application where MFC callback functions use to catch
everythig with catch(...) and then simply call my GlobalErrorHandler()
that will throw; and catch the exception again, this time with a
specific exception type.
That is, GlobalErrorHandler knows about as many exception types as my
application can handle, and all callback functions just catch(...) once
without the specific type and then invoke GlobalErrorHandler(), which
will then get the type.
Recently I run into a strange problem with this approach, however. After
the rethrow the program auto-magically rebuilds the entire call stack
from the time of the original throw, and attempts to destroy local
variables in the call stack again. So my application then crashes.
Why would VC++ call destructors again at re-throw ? How can it rebuild
the call stack ? Has anyone run into this problem before ?
Thank you,
Timothy Madden
Your are not clear enough. If you catch with (...) how does
GlobalErrorHandler know the exception type? could you post a code snippet?
--
Cholo Lennon
Bs.As.
ARG
Timothy Madden
2010-09-24 11:34:54 UTC
Permalink
Post by Cholo Lennon
Post by Timothy Madden
Hello
I have an application where MFC callback functions use to catch
everythig with catch(...) and then simply call my GlobalErrorHandler()
that will throw; and catch the exception again, this time with a
specific exception type.
That is, GlobalErrorHandler knows about as many exception types as my
application can handle, and all callback functions just catch(...) once
without the specific type and then invoke GlobalErrorHandler(), which
will then get the type.
Recently I run into a strange problem with this approach, however. After
the rethrow the program auto-magically rebuilds the entire call stack
from the time of the original throw, and attempts to destroy local
variables in the call stack again. So my application then crashes.
Why would VC++ call destructors again at re-throw ? How can it rebuild
the call stack ? Has anyone run into this problem before ?
Thank you,
Timothy Madden
Your are not clear enough. If you catch with (...) how does
GlobalErrorHandler know the exception type? could you post a code snippet?
You can simply
throw ;
and then you can catch for the specific exception type, which is still
there.

Here is my handler:

void GlobalExceptionHandler(TCHAR const *szContextMessage)
{
&szContextMessage;

try
{
throw;
}
catch (CException *pEx)
{
pEx->ReportError(MB_OK | MB_ICONSTOP);
pEx->Delete();
}
catch (Cancelled const &cancelled)
{
&cancelled;
}
catch (ServerError const &csvr)
{
// Collaboration Server errors carry UTF-8 messages, which should be
decoded as such
ErrorMessage
(
localString(UTF8Decode(localString(csvr.what())))
.operator TCHAR const *()
);
}

// other handlers as needed
}

Anyway trying to give a sample code snipped I found my problem. I guess
this is called Rubber duck debugging :)

I forgot my handler was actually split into two functions: one to catch
for the generic types std::exception and (...), and another one to catch
for some more application-specific types (CException *, Cancelled,
ServerError). The generic handler ReportError() also invokes the
specific handler GlobalExceptionHandler(), so my callback functions only
need to call ReportError() after catch(...).

This time, however, I forgot of the two functions and I only invoked the
specific GlobalExceptionHandler from my catch clause, while the thrown
exception was of the generic std::runtime_error type, which was now
uncaught and my application legally crashed. Actually I supose legally
terminate() should have been called.

Thank you,
Timothy Madden

Loading...