在Python 的try ... except Exception ...
中显示traceback:
1 2 3 4 5 6 |
import traceback try: 1 + 'a' except Exception as e: tb = traceback.format_exc() print(tb) |
输出如下:
1 2 3 |
Traceback (most recent call last): File "<stdin>", line 2, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str' |
这个功能在多层try ... except Exception ...
嵌套的时候特别有用。