Generator::throw

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

Generator::throwÜreteç için bir istisna oluşturur

Açıklama

public function Generator::throw(Throwable $exception): mixed

Üreteç için bir istisna oluşturur ve üretecin kaldığı yerden devam etmesini sağlar. Geçerli yield ifadesine throw $exception deyimini yerleştirmek gibidir.

Bu yöntem çağrıldığında üreteç kapalıysa istisna çağrıcının bağlamında oluşur.

Bağımsız Değişkenler

exception

Üreteç içinde oluşturulacak istisna.

Dönen Değerler

Verilen değeri döndürür.

Örnekler

Örnek 1 - Bir üreteçte istisna yavrulama

<?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'));
?>

Yukarıdaki örneğin çıktısı:

Foo
Exception: Test
Bar