How to print an exception?

Viewed 1249

How to print python exception?

Example:

try:
    action()
except:
    print "Unexpected error:", sys.exc_info()[0]

Prints:

Unexpected error: <type 'exceptions.TypeError'>

It does not have much information for me.

2 Answers

You can print the exception which occurred too.

try:
    action()
except exception as ex:
    print("Exception: " + str(ex))
Related