import traceback def fun(a,b): return a/b try: fun(1,0) except Exception as e: traceback.print_exc() #或者獲得堆棧字符串信息 info = traceback.format_exc()