一、Python异常捕捉
使用try/except捕捉try语句块中的错误,然后让except语句捕获异常信息进行处理。整个语法结构为:
try:
1语句
2语句
except <errorname>:
11语句
22语句 # errorname异常的处理
except <errorname2>:
111语句
222语句 # errorname2异常的处理
else:
3语句
4语句 # 没有异常的执行语句
# error except
>>> try:
... a = 2
... b = 0
... c = a/b
... except:
... print("can not get c!")
... else:
... print("can get c!")
...
can not get c!
# no error
>>> try:
... a = 2
... b = 1
... c = a/b
... except:
... print("can not get c!")
... else:
... print("can get c!")
...
can get c!