嘻皮客娱乐学习网
标题:
介绍一下except的用法和作用
[打印本页]
作者:
luo784
时间:
2014-11-30 16:53
标题:
介绍一下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”)
欢迎光临 嘻皮客娱乐学习网 (http://www.xipick.com/)
Powered by Discuz! X3.3