使用
try:
num = int(input("输入一个整数:"))
result = 8 / num
except ZeroDivisionError:
print("除0错误")
except Exception as e:
print("未知错误 %s" % e)
else:
print("没有异常才会执行")
finally:
print("无论是否异常都会执行")
print("---")
主动抛异常
try:
print("主动抛出异常")
raise Exception("异常") # 关键字raise 抛异常
except Exception as e:
print(e)
print("---")