2018-10-26
阅读量:
1015
介绍一下except的用法和作用?
Python的except用来捕获所有异常, 因为Python里面的每次错误都会抛出 一个异常,所以每个程序的错误都被当作一个运行时错误。
一下是使用except的一个例子:
try:
foo =
opne(”file”) #open被错写为opne
except:
sys.exit(”could not
open
file!”)
因为这个错误是由于open被拼写成opne而造成的,然后被except捕获,所以debug程序的时候很容易不知道出了什么问题
下面这个例子更好点:
try:
foo =
opne(”file”) # 这时候except只捕获IOError
except
IOError:
sys.exit(”could not
open
file”)






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论