Finding out the source of an exception in C++ after it is caught?

I'm looking for an answer in MS VC++.

When debugging a large C++ application, which unfortunately has a very extensive usage of C++ exceptions. Sometimes I catch an exception a little later than I actually want.

Example in pseudo code:

    throw e;


    Function A()
    (<--- breakpoint)

I can catch the exception with a breakpoint when debugging. But I can't trace back if the exception occurred in FunctionA() or FunctionB(), or some other function. (Assuming extensive exception use and a huge version of the above example).

One solution to my problem is to determine and save the call stack in the exception constructor (i.e. before it is caught). But this would require me to derive all exceptions from this base exception class. It would also require a lot of code, and perhaps slow down my program.

Is there an easier way that requires less work? Without having to change my large code base?

Are there better solutions to this problem in other languages?


If you are just interested in where the exception came from, you could just write a simple macro like

#define throwException(message) \
    {                           \
        std::ostringstream oss; \
        oss << __FILE __ << " " << __LINE__ << " "  \
           << __FUNC__ << " " << message; \
        throw std::exception(oss.str().c_str()); \

which will add the file name, line number and function name to the exception text (if the compiler provides the respective macros).

Then throw exceptions using

throwException("An unknown enum value has been passed!");

You pointed to a breakpoint in the code. Since you are in the debugger, you could set a breakpoint on the constructor of the exception class, or set Visual Studio debugger to break on all thrown exceptions (Debug->Exceptions Click on C++ exceptions, select thrown and uncaught options)

There's an excellent book written by John Robbins which tackles many difficult debugging questions. The book is called Debugging Applications for Microsoft .NET and Microsoft Windows. Despite the title, the book contains a host of information about debugging native C++ applications.

In this book, there is a lengthy section all about how to get the call stack for exceptions that are thrown. If I remember correctly, some of his advice involves using structured exception handling (SEH) instead of (or in addition to) C++ exceptions. I really cannot recommend the book highly enough.

Put a breakpoint in the exception object constructor. You'll get your breakpoint before the exception is thrown.

There is no way to find out the source of an exception after it's caught, unless you include that information when it is thrown. By the time you catch the exception, the stack is already unwound, and there's no way to reconstruct the stack's previous state.

Your suggestion to include the stack trace in the constructor is your best bet. Yes, it costs time during construction, but you probably shouldn't be throwing exceptions often enough that this is a concern. Making all of your exceptions inherit from a new base may also be more than you need. You could simply have the relevant exceptions inherit (thank you, multiple inheritance), and have a separate catch for those.

You can use the StackTrace64 function to build the trace (I believe there are other ways as well). Check out this article for example code.

Here's how I do it in C++ using GCC libraries:

#include <execinfo.h> // Backtrace
#include <cxxabi.h> // Demangling

vector<Str> backtrace(size_t numskip) {
    vector<Str> result;
    std::vector<void*> bt(100);
    bt.resize(backtrace(&(*bt.begin()), bt.size()));
    char **btsyms = backtrace_symbols(&(*bt.begin()), bt.size());
    if (btsyms) {
        for (size_t i = numskip; i < bt.size(); i++) {
            Aiss in(btsyms[i]);
            int idx = 0; Astr nt, addr, mangled;
            in >> idx >> nt >> addr >> mangled;
            if (mangled == "start") break;
            int status = 0;
            char *demangled = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);

            Str frame = (status==0) ? Str(demangled, demangled+strlen(demangled)) : 
                                      Str(mangled.begin(), mangled.end());

    return result;

Your exception's constructor can simply call this function and store away the stack trace. It takes the param numskip because I like to slice off the exception's constructor from my stack traces.

There's no standard way to do this.

Further, the call stack must typically be recorded at the time of the exception being thrown; once it has been caught the stack has unrolled, so you no longer know what was going on at the point of being thrown.

In VC++ on Win32/Win64, you might get usable-enough results by recording the value from the compiler intrinsic _ReturnAddress() and ensuring that your exception class constructor is __declspec(noinline). In conjunction with the debug symbol library, I think you could probably get the function name (and line number, if your .pdb contains it) that corresponds to the return address using SymGetLineFromAddr64.

In native code you can get a shot at walking the callstack by installing a Vectored Exception handler. VC++ implements C++ exceptions on top of SEH exceptions and a vectored exception handler is given first shot before any frame based handlers. However be really careful, problems introduced by vectored exception handling can be difficult to diagnose.

Also Mike Stall has some warnings about using it in an app that has managed code. Finally, read Matt Pietrek's article and make sure you understand SEH and vectored exception handling before you try this. (Nothing feels quite so bad as tracking down a critical problem to code you added help track down critical problems.)

I believe MSDev allows you to set break points when an exception is thrown.

Alternatively put the break point on the constructor of your exception object.

If you're debugging from the IDE, go to Debug->Exceptions, click Thrown for C++ exceptions.

Other languages? Well, in Java you call e.printStackTrace(); It doesn't get much simpler than that.

In case anyone is interested, a co-worker replied to this question to me via email:

Artem wrote:

There is a flag to MiniDumpWriteDump() that can do better crash dumps that will allow seeing full program state, with all global variables, etc. As for call stacks, I doubt they can be better because of optimizations... unless you turn (maybe some) optimizations off.

Also, I think disabling inline functions and whole program optimization will help quite a lot.

In fact, there are many dump types, maybe you could choose one small enough but still having more info

Those types won't help with call stack though, they only affect the amount of variables you'll be able to see.

I noticed some of those dump types aren't supported in dbghelp.dll version 5.1 that we use. We could update it to the newest, 6.9 version though, I've just checked the EULA for MS Debugging Tools -- the newest dbghelp.dll is still ok to redistribute.

I use my own exceptions. You can handle them quite simple - also they contain text. I use the format:

throw Exception( "comms::serial::serial( )", "Something failed!" );

Also I have a second exception format:

throw Exception( "comms::serial::serial( )", ::GetLastError( ) );

Which is then converted from a DWORD value to the actual message using FormatMessage. Using the where/what format will show you what happened and in what function.

Need Your Help

Tkinter: get data from a Entry widget

python tkinter

I am workin with Python 2.7. This is the initial part of a longer programme. What I want to do is to add a new username, together with is height and weight. I use a .txt file to store user data,

Libgdx Actor.setBounds() relative to Actor.x and Actor.y and etc

java libgdx

Im having trouble of setting bounding rectangle position thats why i ask this.