Overview:
-
The throw() method enables raising an exception into a generator function from the calling code.
-
With the throw() method, a response can be given back to the generator function from the caller, thus controlling the behaviour of the generator on certain (which are deemed exceptional by the calling code of the generator) conditions.
Example:
# Example Python program that throws an exception # Define OddError # Define a generator function # Define a consumer that makes use of generator function # Value to send to the generator # Create a generator iterator # Start the generator # Use the generator further through f1 |
Output:
8 After generator reset: 8 |