简体   繁体   中英

Getting a stack trace in C++ exceptions

The fact a stack/function trace is supplied in Java/C# exceptions is really useful. Is there a handy way to do this in C++ or would I have to bake extra data into every method/function?

Most debuggers can be set up to pause your program each time an exception is thrown (either any exception or an exception for which there's no handler) so that you can obserevr a call stack.

Also if your code only throws exceptions of classes you control you can put code for dumping the call stack in those classes constructors .

If you are considering adding metdata to your exceptions, You may consider using boost exception handling . boost::exception allows adding information to an exception after it has been thrown.

Some OS APIs provide for stack traces, I know that the Windows API has StackWalk64 or something like that that can do stack tracing.

However, if you can't depend on such a thing, then pretty much all you can do is either ship a debug build, or do it manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM