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. yield from $gen;
  12. }
  13.  
  14. $gen = from();
  15. $gens[] = gen($gen);
  16. $gens[] = gen($gen);
  17.  
  18. foreach ($gens as $g) {
  19. $g->current(); // init.
  20. }
  21.  
  22. foreach ($gens as $i => $g) {
  23. print "Generator: $i\n";
  24. try {
  25. $g->current();
  26. $g->next();
  27. } catch (\ClosedGeneratorException $e) {
  28. print "Caught ClosedGeneratorException\n";
  29. } catch (\Exception $e) {
  30. print "Caught Generic Exception\n";
  31. }
  32. }
  33.  
Success #stdin #stdout 0s 82880KB
stdin
Standard input is empty
stdout
Generator: 0
Caught Generic Exception
Generator: 1
Caught ClosedGeneratorException