#后續代碼可以正常運行
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
異常的傳遞性: