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