try:
f = open('我为什么是一个文件.txt') #这个txt并不存在
print(f.read())
f.close()
sum=1+'1' #文件类型出错
except TypeError as reason:
print('类型出错啦\n错误的原因是:'+str(reason))
except OSError as reason:
print('文件出错了\n错误的原因是:'+str(reason))
如果去掉这两行
except OSError as reason:
print('文件出错了\n错误的原因是:'+str(reason))
会报错
Traceback (most recent call last):
File "E:/PyCharm 2016.3.2/untitled3/guessing/test01.py", line 2, in <module>
f = open('我为什么是一个文件.txt') #这个txt并不存在
FileNotFoundError: [Errno 2] No such file or directory: '我为什么是一个文件.txt'
而如果去掉
except TypeError as reason:
print('类型出错啦\n错误的原因是:'+str(reason))
则不会报错,也就是说忽视了sum=1+‘1’的错误,这个是缺陷吗还是什么,麻烦大咖 解释下
迷茫2017-04-18 10:20:58
There are two errors here. When an OSError occurs, the following code will not be executed, that is, it is not executed.
sum=1+'1' #文件类型出错
, it’s not ignored, it’s just not executed, so no error is reported