From 48caa09a9dd4b53656f58f622cf38341b6c099dc Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 22 Mar 2014 17:50:12 +0200 Subject: objgenerator: Implement .throw() method to throw exceptions into generator. --- tests/basics/generator-exc.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'tests') diff --git a/tests/basics/generator-exc.py b/tests/basics/generator-exc.py index 601fa2c00..09aca5d9a 100644 --- a/tests/basics/generator-exc.py +++ b/tests/basics/generator-exc.py @@ -29,3 +29,25 @@ try: print(next(g)) except StopIteration: print("StopIteration") + + +# Test throwing exception into generator +def gen3(): + yield 1 + try: + yield 2 + except ValueError: + print("ValueError received") + yield 3 + yield 4 + yield 5 + +g = gen3() +print(next(g)) +print(next(g)) +print("out of throw:", g.throw(ValueError)) +print(next(g)) +try: + print("out of throw2:", g.throw(ValueError)) +except ValueError: + print("Boomerang ValueError caught") -- cgit v1.2.3