Hi !

Originally posted message over here:


> Hello everybody!

The feature of getting stacktraces from exceptions was requested in
https://lists.isocpp.org/std-proposals/2020/11/2086.php and in the "2021
Annual C++ Developer Survey".

I've created a prototype for getting stacktraces from exceptions

More details can be found in the paper D2370R0 "Stacktrace from exception"
here -

Any comments are welcome!
vRegards, Andrei

C# in it's design does not allow only to catch exception, but also to survive from it.

The mechanics goes deeper in handling such events as DispatcherUnhandledException 


and deeper into C#, like HandleProcessCorruptedStateExceptions special kind of attributes.

But I was wondering whether same kind of exception handling system could be implemented in c++.

No matter what kind of exception occurs - for example assignment to nullptr, (https://github.com/tapika/stacktrace/blob/develop/example/cpp_crashy_managed_dll/cpp_crashy_managed_dll.cpp#L16)
Jump by null pointer - https://github.com/tapika/stacktrace/blob/develop/example/cpp_crashy_managed_dll/cpp_crashy_managed_dll.cpp#L23

C++ application would be able to survive it.

Initial idea of how low level exception could be implemented in illustrated in this header file:
and sample code:

Where you get exception code and exception textual representation if you want to log it.

Exception codes and their description could be also generalized - I have just listed all for windows and linux:

but I think null pointer exception code for windows is EXCEPTION_ACCESS_VIOLATION and for linux SIGSEGV - but that could be abstracted behind common exception code
and common description message.

There are more problems on the way - especially if you mix C++ and C# code, then windows C++ implementation might need to ignore special .net framework codes

But this is something Microsoft team will need to work on.

More information for Microsoft team, Windows port:

- There exists a way to probe at run-time whether application is pure C++ or mixed mode C++.

- Breakpoints not always work with my own approach (https://github.com/tapika/stacktrace/blob/develop/src/exception_handler.cpp#L133)

- C# re-throw will be catched by C++ exception handler (https://github.com/dotnet/diagnostics/issues/151#issuecomment-756894271)

But I think we can clearly define interface, and let Microsoft worry about implementation details later on.

So would it be possible to add following use case:

Application can detect abnormal application behavior and survive from it without terminating the process.
Call stack determination might be used when such low level exception happens.

-- Have a nice day!