python -Exception

#系统异常信息捕获   try....except...as....else  、try....except...as....finally

try:
#输入文件名称,打开文件
filename=input("input fileName:")
open("%s.txt"%filename)
#BaseException 后加as可获取系统异常信息
except BaseException as msg:
print(msg)
#没有异常就打印else
else:
print(filename)
#不论是否有异常都执行finally内容
finally:
print("the end!")

Try是执行过程中捕获异常,raise是先定义一个标准,不符合条件就抛出异常(raise必须接python标准异常类,如
ZeroDivisionError)
def div(x,y):
if y==0:
raise ZeroDivisionError("zeroisnotallow")
return x/y
try:
div(8,0)
except BaseException as msg:
print(msg)


posted @ 2019-11-15 15:15  ksaanni  阅读(177)  评论(0)    收藏  举报