异常处理:
1、except Exception as e:
try:
except Exception as e:
print("错误信息:", e)
2、traceback.print_exc()
import traceback
def func(num1, num2):
# try:
x = num1 * num2
y = num1 / num2
return x, y
# except:
# traceback.print_exc()
try:
func(1, 0)
except:
# 捕捉异常,并将异常传播信息输出控制台
traceback.print_exc()
# 捕捉异常,并将异常传播信息输出指定文件中
traceback.print_exc(file=open('log.txt', 'a'))
traceback.print_exc(file=open('error.txt', 'w+', encoding='utf-8'))