Handle generator exceptions in its consumer

前端 未结 8 1626
迷失自我
迷失自我 2020-11-29 10:07

This is a follow-up to Handle an exception thrown in a generator and discusses a more general problem.

I have a function that reads data in different formats. All fo

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 10:41

    You can return a tuple of record and exception in the parsefunc and let the consumer function decide what to do with the exception:

    import random
    
    def get_record(line):
      num = random.randint(0, 3)
      if num == 3:
        raise Exception("3 means danger")
      return line
    
    
    def 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
    

提交回复
热议问题