



python
#后续代码可以正常运行
try:
f= open("xxx.txt","r",encoding='utf-8')
except:
print("except error")
python
#捕获指定异常,其他异常报错程序中止,管不到
try:
print(name)
except NameError as you_call:
print("name error")
python
#打印异常
try:
print(name)
except NameError as you_call:
print(f"name error {you_call}")
name error name 'name' is not defined

python
try:
f=1/0
except (NameError,ZeroDivisionError) as you_call:
print(f"name or math error {you_call}")
name or math error division by zero
try:
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
python
#捕获所有的异常(某行代码出错就会被捕获到)
import math
try:
math.e()
except Exception as e:
print(f"error {e}")
error 'float' object is not callable




异常的传递性:



