implementing uncaught_exception in MSVC++ 6.0

I've regressed to using VC++ 6.0, now that runtime does not implement uncaught_exception properly
see KB: 242192 - BUG: uncaught_exception() Always Returns False

But I need it!

Is there a place where a genuine implementation exist ?

I'm trying to implement a stack trace for our software.
at the start of every function programmers will instanciate a StackTrace object, when an exception is thrown, the StackTrace object in it's destructor will test uncaught_exception and log info if it finds that it is destroyed as part of an exception.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hmm, I can only think of a workaround like

#define st_throw(x) { st.set_throw (); throw(x)}

void foo () {

    StackTrace st;

    // some code, some error condition

    st_throw ( new CThisOrThatException ());

and evaluate whether 'set_throw ()' indicated an error state. That's ugly, I know, but really implementing 'uncaught_exception()' seems not really easy to me...

BTW: This issue is said to be fixed with VC++ 7.1
DAndersonAuthor Commented:
Nice try!

won't work for me, I'm stuck with 6.0 because tons of legacy
code of a running system . Cannot recompile everything (some throwing functions are in libraries, no source code) same with stl & MFC.

I thought there could be a way to hook into the exception handling mechanism to set the flag. Microsoft do hook into them as they manage to catch exceptions thrown.

I hate those brain damage compilers!

>>I thought there could be a way to hook into the exception handling mechanism to set the flag

That's what was my 1st thought, too - however, going through the CRT sources that ship with VC++ yields no clue on how one could do that. 'throw()' isn't a simple function, it is a language construct that makes the compiler generate implementation specific code...
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

DAndersonAuthor Commented:
Yes, throwing & catching is a language construct, but implemented with some low level functions in the libs

There is:

I thought that maybe someone would have rewritten them to properly set the uncaught flag.

You are certainly right about that - but, if it was that easy to fix the problem, MS would have done that with a service pack (OK, I am being pessimistic here, but just intercepting the aforementioned functions sounds too easy...)
For what it's worth, I recall, for having worked a while on that source code tree, that VirtualDub ( includes what seemed (at least, to me) to be a quite powerful exception handling system. The application is able to produce stack traces and disassembly listings when it crashes. The "system" is quite complex, involving special build steps (to convert symbols and maps), relies on APIs like SetUnhandledExceptionFilter (so, it's not really a C++ solution), and if I recall correctly, there's a good deal of assembly. It might be challenging to port that mechanism into another application framework, but I'm confident that the study of that source code is worth the time. Maybe not quite what you're looking for, but I thought I'd mention it in case of.
DAndersonAuthor Commented:
I've found a way, now maybe I'm missing something.
so if someone think it it not appropriate let me know

In my stack trace constructor, I register the stack frame (ebp) of the calling function
in the destructor I check if the stack frame of the calling function is the same as the one in the constructor.

if they are the same, it means normal unwinding
if not it means exception unwinding so it is time to log!

What do you think ?

>>if they are the same, it means normal unwinding
>>if not it means exception unwinding so it is time to log!

Hmm, if the exception occurs inside the function, the frame pointer will be the same, regardless of onwinding or not...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DAndersonAuthor Commented:
> Hmm, if the exception occurs inside the function, the frame pointer will be the same,
> regardless of onwinding or not...

the handling is not done at the throw point, but within some runtime function
so the stack is different when the destructor is called.

here is pseudo code of thow:

   ThrowingStackFrame = GetTrhowingStackFrame();
   HandlingStackFrame = FindHandlingStackFrame();
    ListOfObjectToDestroy = buildListOfObjectToDestroy(ThrowingStackFrame, HandlingStackFrame);
   For Each Object call destructor;    // HERE STACK of thow function!!!!

DAndersonAuthor Commented:
jkr, you are right, and I was wrong
ebp is the same, but really I was relying on esp. But I found a problem and sometime esp change within a function: I had forgotten _alloca and other tricks compiler is allowed to play with registers

In debug mode the code for functionning properly, but in release mode with optimised for speed few functions fail

So I cannot reliably find out if I am within an exception context. Bummer.

I guess I'll have to push to upgrade to latest version of  microsoft C++.

Thanks you all for your time

No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: jkr {http:#9846312}

Please leave any comments here within the next seven days.

EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.