What is generator.throw() good for?

Viewed 11327

PEP 342 (Coroutines via Enhanced Generators) added a throw() method to generator objects, which allows the caller to raise an exception inside the generator (as if it was thrown by the yield expression).

I am wondering what the use cases for this feature are.

4 Answers

This "answer" is more like a trivia.

We can (ab)use the generator's throw() to raise Exception inside a lambda, which does not otherwise support the raise statement.

foo = lambda: (_ for _ in ()).throw(Exception('foobar'))

Quoted from https://stackoverflow.com/a/8294654/728675

Related