您可以在parsefunc中返回记录和异常的元组,并让使用者函数决定如何处理异常:
import randomdef get_record(line): num = random.randint(0, 3) if num == 3: raise Exception("3 means danger") return linedef parsefunc(stream): for line in stream: try: rec = get_record(line) except Exception as e: yield (None, e) else: yield (rec, None)if __name__ == '__main__': with open('temp.txt') as f: for rec, e in parsefunc(f): if e: print "Got an exception %s" % e else: print "Got a record %s" % rec
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)