How to print an exception in Python?

TIMEX picture TIMEX · Sep 27, 2009 · Viewed 940.2k times · Source
try:
    something here
except:
    print('the whatever error occurred.')

How can I print the error/exception in my except: block?

Answer

jldupont picture jldupont · Sep 27, 2009

For Python 2.6 and later and Python 3.x:

except Exception as e: print(e)

For Python 2.5 and earlier, use:

except Exception,e: print str(e)