#后续代码可以正常运行
try:f= open("xxx.txt","r",encoding='utf-8')except:print("except error")
#捕获指定异常,其他异常报错程序中止,管不到
try:print(name)
except NameError as you_call:print("name error")
#打印异常
try:print(name)
except NameError as you_call:print(f"name error {you_call}")name error name 'name' is not defined
try:f=1/0
except (NameError,ZeroDivisionError) as you_call:print(f"name or math error {you_call}")name or math error division by zerotry:print(name)
except (NameError,ZeroDivisionError) as you_call:print(f"name or math error {you_call}")name or math error name 'name' is not defined
#捕获所有的异常(某行代码出错就会被捕获到)
import mathtry:math.e()
except Exception as e:print(f"error {e}")error 'float' object is not callable
异常的传递性: