我在提出例外时遇到困难,例如:
import csv
o = open('/home/foo/dummy.csv', 'r') # Empty file!
reader = csv.reader(o, delimiter=';')
reader = list(reader)
try:
for line in reader:
print line[999] # Should raise index out of range!
except Exception, e:
print e
基本上 csv.reader 读取空文件,转换为空列表,上面的代码应该打印 IndexError。但事实并非如此。然而,下面的代码完美地提出了:
print reader[0][999]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
难道我做错了什么?