"""标签:try,name,未定义,python,捕获,学习,print,异常 From: https://www.cnblogs.com/ashuai123/p/17375603.html
异常:
捕获异常:
基本语法:
try:
可能发生错误的代码
except:
出现异常执行代码
else:
没异常,执行
finally:
无论有无异常,都执行
异常具有传递性
"""
# 捕获指定异常
try:
print(name)
except NameError as e:
print("出现未定义异常")
print(e)
# 捕获多个异常
try:
print(name)
1/0
except (NameError, ZeroDivisionError) as e:
print("出现未定义异常 或 除0异常")
print(e)
# 捕获所有异常
try:
print(name)
1/0
except Exception as e:
print("出现未定义异常 或 除0异常")
print(e)