fork download
  1. <?php
  2.  
  3. class CustomException extends Exception {}
  4.  
  5. function from() {
  6. yield 1;
  7. throw new CustomException();
  8. }
  9.  
  10. function gen($gen) {
  11. try {
  12. yield from $gen;
  13. } catch (\ClosedGeneratorException $e) {
  14. yield "Caught ClosedGeneratorException";
  15. } catch (\Exception $e) {
  16. yield "Caught Generic Exception";
  17. }
  18. }
  19.  
  20. $gen = from();
  21. $gens[] = gen($gen);
  22. $gens[] = gen($gen);
  23.  
  24. foreach ($gens as $g) {
  25. $g->current(); // init.
  26. }
  27.  
  28. foreach ($gens as $i => $g) {
  29. print "Generator: $i\n";
  30. print $g->current()."\n";
  31. $g->next();
  32. }
  33.  
Success #stdin #stdout 0.01s 82880KB
stdin
Standard input is empty
stdout
Generator: 0
1
Generator: 1
Caught ClosedGeneratorException