generator = getstuff()
print('Get some numbers...')
print(next(generator))
print(next(generator))
print(next(generator))
print('Oh, actually, I want squares!')
print(generator.throw(ValueError))
print(next(generator))
print(next(generator))
Raising exception in a generator, handle it elsewhere and vice versa in python