How can you use CaptureStackBackTrace to capture the exception stack, not the calling stack?

前端 未结 3 802
终归单人心
终归单人心 2020-12-04 22:41

I marked up the following code:

#include \"stdafx.h\"
#include 
#include 
#include 
#include \"dbghelp.h\"
         


        
3条回答
  •  抹茶落季
    2020-12-04 23:24

    If you wanted to capture the stack backtrace of the point where the code threw an exception, you must capture the stack backtrace in the ctor of the exception object and store it within the exception object. Hence the part calling CaptureStackBackTrace() should be moved to the constructor of the exception object, which should also provide methods to fetch it either as a vector of addresses or as a vector of symbols. This is exactly how Throwable in Java and Exception in C# operate.

    Finally, please do not write:

    throw new exception;
    

    in C++, as you would in C# or Java. This is an excellent way to both produce memory leaks and to fail to catch the exceptions by type (as you are throwing pointers to these types). Rather use:

    throw exception();
    

    I'm aware that this is an old question but people (including myself) are still finding it.

提交回复
热议问题