2

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.

martineau
  • 112,593
  • 23
  • 157
  • 280

2 Answers2

5

Use traceback module:

try:
    action()
except:
    import traceback
    traceback.print_exc()
phd
  • 69,888
  • 11
  • 97
  • 133
0

You can print the exception which occurred too.

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