// g++-8 -Wall -g -o exception exception.cpp #include #include int main2(); #include #include #include /* Obtain a backtrace and print it to stdout. https://www.gnu.org/software/libc/manual/html_node/Backtraces.html */ void print_trace (void) { void *array[10]; size_t size; char **strings; size_t i; size = backtrace (array, 10); strings = backtrace_symbols (array, size); printf ("Obtained %zd stack frames.\n", size); for (i = 0; i < size; i++) printf ("symbols %s\n", strings[i]); free (strings); } int main() { try { main2(); } catch( const std::exception &e) { const std::string what(e.what()); std::cout << "Unhandled exception: [" << what << "]\n"; print_trace(); exit(0); } } int main2() { std::vector v; return v.at(0); }