下面是我的代码,我的while循环调用我的函数(function_called)来处理日志文件。当我的函数处理每一行时出现异常时,它应该忽略其余代码并将控制权转回while循环以转到下一行。
所以它应该是:
- 获取线 - - 如果行中的索引错误异常 - - 代码 - - 移到下一行
我的代码概述如下所述
def function_called():
try:
--Something is happening here--
except IndexError:
--If there is an exception then code needs to be ignored
and it should be transfered to the while loop to go to the next line--
f = open('/data/qantasflight/run/tomcat/logs/localhost_access_log.2016-03-31.txt', 'r')
while True:
line = ''
while len(line) == 0 or line[-1] != '\n':
tail = f.readline()
if tail == '':
continue
line = tail
print (line, end='')
# print (line)
sleep(0.1)
function_called(line)
答案 0 :(得分:0)
您应该在要处理它的地方捕获异常,即在while
循环中。
def function_called():
""" something, which might raise an index error """
while True:
""" do stuff """
try:
function_called(line)
except IndexError:
continue
如果您想在function_called
中进行更明确的错误处理,则可以执行此操作并再次raise
例外:
def function_called():
try:
""" something, which might raise an index error """
except IndexError:
""" more error handling """
raise
查看chapter about error handling in the official documentation。