mpyw\Co\Internal\GeneratorContainer::throw_ PHP Method

throw_() public method

Throw exception into generator.
public throw_ ( Throwable | Exception $e )
$e Throwable | Exception
    public function throw_($e)
    {
        $this->validateValidity();
        try {
            $this->g->throw($e);
            return;
        } catch (\Throwable $e) {
        } catch (\Exception $e) {
        }
        $this->e = $e;
    }

Usage Example

示例#1
0
 public function testExternalException()
 {
     $gen = (function () {
         $this->assertInstanceOf(\RuntimeException::class, (yield CoInterface::SAFE => null));
         (yield null);
     })();
     $con = new GeneratorContainer($gen);
     $con->key() === CoInterface::SAFE ? $con->send(new \RuntimeException()) : $con->throw_(new \RuntimeException());
     $this->assertTrue($con->valid());
     $this->assertFalse($con->thrown());
     $gen = (function () {
         (yield null);
         (yield null);
     })();
     $con = new GeneratorContainer($gen);
     $con->key() === CoInterface::SAFE ? $con->send(new \RuntimeException()) : $con->throw_(new \RuntimeException());
     $this->assertFalse($con->valid());
     $this->assertTrue($con->thrown());
     $this->assertInstanceOf(\RuntimeException::class, $con->getReturnOrThrown());
 }