def trap_exceptions ( results , handler , exceptions = Exception ) : try : for result in results : yield result except exceptions as exc : for result in always_iterable ( handler ( exc ) ) : yield result
python yield catch except
Iterate through the results but if an exception occurs stop processing the results and instead replace the results with the output from the exception handler .
def trap_exceptions(results, handler, exceptions=Exception):
	"""
	Iterate through the results, but if an exception occurs, stop
	processing the results and instead replace
	the results with the output from the exception handler.
	"""
	try:
		for result in results:
			yield result
	except exceptions as exc:
		for result in always_iterable(handler(exc)):
			yield result