如何在 Python 中打印异常?

问题描述:

try:
    something here
except:
    print('the whatever error occurred.')

如何在我的 except: 块中打印错误/异常?

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

对于 Python 2.6 及更高版本和 Python 3.x:

For Python 2.6 and later and Python 3.x:

except Exception as e: print(e)

对于 Python 2.5 及更早版本,请使用:

For Python 2.5 and earlier, use:

except Exception,e: print str(e)