# throw

Generator::throw

Throw an exception into the generator

# 

ENTITY

<div data-wrapper="1" role="Generator">

public

</div>

<div data-wrapper="1" role="Generator">

mixed

</div>

<div data-wrapper="1" role="Generator">

Generator::throw

</div>

<div data-wrapper="1" role="Generator">

Throwable

</div>

<div data-wrapper="1" role="Generator">

exception

</div>

Throws an exception into the generator and resumes execution of the generator. The behavior will be the same as if the current ENTITY expression was replaced with a `throw $exception` statement.

If the generator is already closed when this method is invoked, the exception will be thrown in the caller's context instead.

# 

ENTITY

`exception`  
Exception to throw into the generator.

# 

ENTITY

Returns the yielded value.

# 

ENTITY

<div class="example">

<div class="title">

Throwing an exception into a generator

</div>

```
<?php
function gen() {
    echo "Foo\n";
    try {
        yield;
    } catch (Exception $e) {
        echo "Exception: {$e->getMessage()}\n";
    }
    echo "Bar\n";
}
 
$gen = gen();
$gen->rewind();
$gen->throw(new Exception('Test'));
?>

    
```

ENTITY

    Foo
    Exception: Test
    Bar

        

</div>