You can't use try
/catch
, because those require a correct program, and the crashes you experience are because your program is ill-formed/incorrect/broken. You cannot in general use deterministic programming to work around non-deterministic broken code. Avoid writing broken code in the first place (use tools like Clang's asan/tsan/ubsan, and Valgrind, and write tests), or use a language that doesn't become ill-formed when you make programming errors (like Java or Python).
What normally happens is that when the operating system kills your process because of some illegal operation (e.g. illegal instructions or illegal memory access), it creates a core dump of the process just before it gets removed from the system. The core dump consists of the entire memory content (more or less), and it includes the stack traces of all the running threads.
Some contemporary OSs pipe the coredump to a program that handles it, e.g. by uploading it to a software vendor for analysis. You could do something similar, though you'll have to arrange that with your OS. It's probably enough just to send the stack traces of the running threads rather than the entire memory, though.
You can also create stack traces of a running program, either by using a library (libunwind?) or by attaching a debugger, which interrupts the program, but there's generally not much use for this - the only stack trace that is interesting is the one from where the illegal operation happened, because you want to know what that operation was.