Generic catch for python

Solution 1:

Exceptions are already printed by default before program termination. If you want to send the error somewhere else (not print it) you can do this:

try:
    something()
except Exception as e:
    send_somewhere(traceback.format_exception(*sys.exc_info()))
    raise # reraises the exception

note that this format using the as keyword is for python > 2.6. The old way was:

except Exception, e:

Solution 2:

The traceback module is quite useful for formatting tracebacks. You can then write it to a logfile.

Solution 3:

Does this work? :

except BaseException, e: