The PHP Generator::throw API performs a focused operation on an object or value. The examples below show its effect on actual data.
Injects a Throwable into a suspended Fiber or Generator at the point where it resumes.
What Is Generator::throw in PHP?
Injects a Throwable into a suspended Fiber or Generator at the point where it resumes.
Syntax:
Generator::throw(Throwable $exception): mixed
The method accepts the following parameters:
$exception— Required. A Throwable parameter named exception; the examples below show a supported value.
Return type declared by PHP: mixed. The value or side effect is demonstrated in the examples below.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
$generator = (function (): Generator {
yield "alpha" => 10;
$response = yield "beta" => 20;
return $response ?? "complete";
})();
try { $generator->throw(new RuntimeException("Stop")); } catch (RuntimeException $error) { echo $error->getMessage(); }
Output:
Stop
Injects a Throwable into a suspended Fiber or Generator at the point where it resumes.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
$generator = (function (): Generator {
yield "east" => 15;
$response = yield "west" => 35;
return $response ?? "complete";
})();
try { $generator->throw(new RuntimeException("Cancelled")); } catch (RuntimeException $error) { echo $error->getMessage(); }
Output:
Cancelled
A generator generally cannot be rewound after progressing beyond its first yield; getReturn() requires completion.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
$generator = (function (): Generator {
yield "east" => 25;
$response = yield "west" => 55;
return $response ?? "complete";
})();
try { $generator->throw(new RuntimeException("Interrupted")); } catch (RuntimeException $error) { echo $error->getMessage(); }
Output:
Interrupted
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- A generator generally cannot be rewound after progressing beyond its first yield; getReturn() requires completion.
- Injects a Throwable into a suspended Fiber or Generator at the point where it resumes.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
Generator::throw | Injects a Throwable into a suspended Fiber or Generator at the point where it resumes. |
| ArrayIterator | ArrayIterator traverses existing array elements; generators produce values lazily and generally cannot be restarted. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of Generator::throw | A generator generally cannot be rewound after progressing beyond its first yield; getReturn() requires completion. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | ArrayIterator traverses existing array elements; generators produce values lazily and generally cannot be restarted. |
Key Takeaways
- Injects a Throwable into a suspended Fiber or Generator at the point where it resumes.
- A generator generally cannot be rewound after progressing beyond its first yield; getReturn() requires completion.
- Use the example that matches the values, runtime version and dependencies in your application.
Generator::throw is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.