python异常处理

# --*-- coding: utf-8 --*--
# create by xiaocaiji
#异常处理父类会覆盖子类,多层嵌套错误也能检测
#格式中else也可以换成finally,意味无论前面有没有错误,finally中语句都要执行,常用语文件操作
'''
格式1:
try:
pass
ecpect NameError as e:
pass
else:
pass


格式2:
try:
pass
ecpect :
pass
else:
pass

格式3:
try:
pass
ecpect (NameError1,NameError2....):
pass
else:
pass

'''

# try:
# print(3/0)
# except:
# print("error")
#
# print("******")


# try:
# print(3/0)
# except ZeroDivisionError as e:
# print("ZeroDivisionError")
# else:
# print("no_error")
# print("haha")


try:
print(3/0)
except (ZeroDivisionError,NameError):
print("ZeroDivisionError or NameError")
else:
print("no_error")
print("haha")

posted on 2019-04-25 17:06  泡面小网管  阅读(128)  评论(0编辑  收藏  举报

导航