I’m using Twister to build a server. I am also maintaining a server error log. The issue is that if I let an exception run all the way up the stack, it’ll crash the current connection and disconnect the user, so obviously I attach a bare except to grab everything else.
Once I’ve caught something, is there a way to get the traceback as a string so that I can store it somewhere/print it myself without raising it and letting Python print it for me once the program crashes?
The traceback module contains some helper functions for printing and inspecting the traceback (for exameble, traceback.print_tb ) – but the important thing is that the traceback information itself is stored in a “interpreter global” variable – sys.exc_traceback, on the module sys.
Quoting from:
http://docs.python.org/reference/compound_stmts.html#try
You can pass the sys.exc_traceback object as a parameter to traceback.print_tb to have the traceback printed to stdout within the except clause.