Handle generator exceptions in its consumer

前端 未结 8 1624
迷失自我
迷失自我 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:45

    (I answered the other question linked in the OP but my answer applies to this situation as well)

    I have needed to solve this problem a couple of times and came upon this question after a search for what other people have done.

    One option- which will probably require refactoring things a little bit- would be to simply create an error handling generator, and throw the exception in the generator (to another error handling generator) rather than raise it.

    Here is what the error handling generator function might look like:

    def err_handler():
        # a generator for processing errors
        while True:
            try:
                # errors are thrown to this point in function
                yield
            except Exception1:
                handle_exc1()
            except Exception2:
                handle_exc2()
            except Exception3:
                handle_exc3()
            except Exception:
                raise
    

    An additional handler argument is provided to the parsefunc function so it has a place to put the errors:

    def parsefunc(stream, handler):
        # the handler argument fixes errors/problems separately
        while not eof(stream):
            try:
                rec = read_record(stream)
                do some stuff
                yield rec
            except Exception as e:
                handler.throw(e)
        handler.close()
    

    Now just use almost the original read function, but now with an error handler:

    def read(stream, parsefunc):
        handler = err_handler()
        for record in parsefunc(stream, handler):
            do_stuff(record)
    

    This isn't always going to be the best solution, but it's certainly an option, and relatively easy to understand.

提交回复
热议问题