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?
3 Answers
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.

- 1
- 1

- 167,383
- 100
- 513
- 979
-
-
@John: The second paragraph is about that - actually the whole question I link to is about that. – sharptooth Dec 30 '10 at 11:41
-
I think the question is then "what code should be in the constructors of exception classes to get a stack trace"? – Mr. Boy Dec 30 '10 at 11:43
-
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.

- 1,577
- 8
- 10
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.

- 144,682
- 38
- 256
- 465