Python中无法捕获的异常

tel*_*t99 1 python exception

问题出现在这个问题中,我将在这段代码中重述:

import csv
FH = open('data.csv','wb')
line1 = [97,44,98,44,99,10]
line2 = [100,44,101,44,102,10]
for n in line1 + line2:
    FH.write(chr(n))
FH.write(chr(0))
FH.close()

import _csv

FH = open('data.csv')
reader = csv.reader(FH)
for line in reader:
    if '\0' in line:  continue
    if not line:  continue
    try:
        print line
    except _csv.Error:
        print 'error'
Run Code Online (Sandbox Code Playgroud)

运行:

$ python test.py 
['a', 'b', 'c']
['d', 'e', 'f']
Traceback (most recent call last):
  File "test.py", line 14, in <module>
    for line in reader:
_csv.Error: line contains NULL byte
Run Code Online (Sandbox Code Playgroud)

所以,我想在文件中包含NUL会导致"无法捕获"的异常.

问题是,除了首先清理文件之外,处理这个问题的最佳方法是什么?"无法捕获"的例外有多常见?

jld*_*ont 7

您没有将"try"块放在正确的位置以捕获此异常.换句话说,这个例外是"可捕获的",只是重新审视你引用的问题.

追溯清楚地表明问题与"for"声明一致.